Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
DotNetBrowser API provides functionality that allows accessing and executing JavaScript code on the loaded web page.
Note: To access JavaScript make sure that web page is loaded completely and JavaScript support is enabled.
DotNetBrowser provides two ways for JavaScript code execution.
Executing JavaScript
To execute JavaScript code asynchronously, without blocking current thread execution until the code is executed and ignore return value, use the Browser.ExecuteJavaScript(String javaScript)
method. This method tells Chromium engine to execute the given JavaScript code asynchronously. The return value of JavaScript code execution is ignored.
The following code updates document.title
property with "My title" value:
С#
browser.ExecuteJavaScript("document.title = 'My title');");
VB.NET
browser.ExecuteJavaScript("document.title = 'My title');")
Executing JavaScript and Return Value
To execute JavaScript code and return value use theBrowser.ExecuteJavaScriptAndReturnValue(String javaScript)
method. This method blocks current thread execution and waits until the code is executed. The result of execution is stored in the JSValue object.
The following code updates the document.title
property with "My title" value and returns the JSValue object with a string value that represents document.title
value:
С#
JSValue title = browser.ExecuteJavaScriptAndReturnValue( "document.title = 'My title'; document.title"); Console.WriteLine("title = " + title.GetString());
VB.NET
Dim title As JSValue = browser.ExecuteJavaScriptAndReturnValue( "document.title = 'My title'; document.title") Console.WriteLine("title = " + title.GetString())
Accessing JavaScript Objects
You can access JavaScript objects on the loaded web page using theBrowser.ExecuteJavaScriptAndReturnValue(String javaScript)
method. If return value represents JavaScript object, then JSValue will contain JSObject instance that represents .NET wrapper for the JavaScript object. JSObject class provides functionality that allows working with JavaScript object properties and calling its functions.
Getting properties
To access JavaScript object property use the JSObject.GetProperty(String name)
method. The following code demonstrates how to get the value of the document.title
property:
С#
JSValue document = browser.ExecuteJavaScriptAndReturnValue("document"); JSValue titleValue = document.AsObject().GetProperty("title"); String title = titleValue.GetString();
VB.NET
Dim document As JSValue = browser.ExecuteJavaScriptAndReturnValue("document") Dim titleValue As JSValue = document.AsObject().GetProperty("title") Dim title As String = titleValue.GetString()
Setting properties
To modify JavaScript object property with specified value use theJSObject.SetProperty(String name, object value)
method. The following code demonstrates how to modify the document.title
property with "My title" value:
С#
JSValue document = browser.ExecuteJavaScriptAndReturnValue("document"); document.AsObject().SetProperty("title", "My title");
VB.NET
Dim document As JSValue = browser.ExecuteJavaScriptAndReturnValue("document") document.AsObject().SetProperty("title", "My title")
Working with Functions
JavaScript object property can represent a function (JSFunction). You can invoke JavaScript object function using the following approach:
С#
JSValue document = browser.ExecuteJavaScriptAndReturnValue("document"); JSValue write = document.AsObject().GetProperty("write"); write.AsFunction().Invoke(document.AsObject(), "<html><body>Hello</body></html>");
VB.NET
Dim document As JSValue = browser.ExecuteJavaScriptAndReturnValue("document") Dim write As JSValue = document.AsObject().GetProperty("write") write.AsFunction().Invoke(document.AsObject(), "<html><body>Hello</body></html>")
Working with Arrays
JSValue can represent an Array (JSArray). You can access elements of the array using the following approach:
С#
JSValue array = browser.ExecuteJavaScriptAndReturnValue("['John', 'Doe', 46];"); JSValue john = array.AsArray()[0]; JSValue doe = array.AsArray()[1];
VB.NET
Dim array As JSValue = browser.ExecuteJavaScriptAndReturnValue("['John', 'Doe', 46];") Dim john As JSValue = array.AsArray().Item(0) Dim doe As JSValue = array.AsArray().Item(1)
.NET to JavaScript Types Conversion
JavaScript and .NET work with different primitive types. DotNetBrowser implements automatic types conversion from .NET to JavaScript types. Here's how .NET objects will be converted to their JavaScript equivalents by DotNetBrowser:
.NET | JavaScript |
---|---|
double, float, long, int, short, System.Double, System.Float System.Int64, System.Int32, System.Int16 | Number |
bool, System.Boolean | Boolean |
System.String | String |
null | null |
DotNetBrowser.JSObject | Object |
DotNetBrowser.JSArray | Array |
DotNetBrowser.JSFunction | Function |