Skip to content

Commit 4c958cd

Browse files
committed
Fixed a much better example for the eval/exec JS stuff. Closes #209.
1 parent 30564e5 commit 4c958cd

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

CefSharp.Example/Resources/Home.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,36 @@ <h3 id="features-javascript-integration">JavaScript integration</h3>
189189
<p>
190190
If you are reading this page in either one of the <strong>CefSharp.Wpf.Example</strong> or
191191
<strong>CefSharp.WinForms.Example</strong> sample applications, you can use the boxes on the right side
192-
of the screen to run arbitrary JavaScript code towards the context of this page. The C# code for performing
193-
these kinds of interactions are quite simple. Like this:
192+
of the screen to run arbitrary JavaScript code towards the context of this page. By default, the content
193+
of the block below will be modified/inspected by the script code.
194+
195+
<pre id="modify-me">You can modify the value of this text field using JavaScript!</pre>
196+
197+
The C# code for performing these kinds of interactions is quite simple. Like this:
194198
</p>
195199

196200
<pre data-shbrush="csharp">
197201
webBrowser.ExecuteScriptAsync(someScriptCode);
198202
</pre>
199-
203+
200204
<p>
201205
The code above will run the provided JavaScript snippet (which may do interesting things, like
202206
interrogating or modifying the DOM of the page, just to name one example out of many potential ones). The
203207
execution is of the "fire-and-forget" style; any result of the execution is silently disregarded. The
204208
execution is also <em>asynchronous</em> in nature, a term which means that (among other things) the method
205209
may return before the actual code has actually been executed.
206210
</p>
207-
211+
208212
<p>
209213
This is the preferrably approach if possible, since it does not deadlock the UI in any way. However, we
210214
realize that it's not suitable for all scenarios. Have faith &mdash; there is a solution even for cases
211215
where you <em>do</em> need to return a value. Just write your code like this:
212216
</p>
213-
217+
214218
<pre data-shbrush="csharp">
215219
var result = webBrowser.EvaluateScript("10 + 20");
216220
</pre>
217-
221+
218222
<p>
219223
Please note that only a limited number of data types are supported when returning the result above. Simple
220224
value types (int, float, etc) and strings all work, but do not expect to be able to return other JavaScript

CefSharp.Wpf.Example/Views/Main/MainView.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
Grid.Row="0"
8383
AcceptsReturn="True"
8484
Margin="6"
85-
Text="alert('Hello World, do you copy?')">
85+
Text="$('#modify-me').text('See how easy that was?')">
8686
<TextBox.InputBindings>
8787
<KeyBinding Key="Enter"
8888
Modifiers="Control"
@@ -111,7 +111,7 @@
111111
Grid.Row="0"
112112
Margin="6"
113113
AcceptsReturn="True"
114-
Text="$('body').children().length">
114+
Text="$('#modify-me').text()">
115115
<TextBox.InputBindings>
116116
<KeyBinding Key="Enter"
117117
Modifiers="Control"

0 commit comments

Comments
 (0)