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');");

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());

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();

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.titleproperty with "My title" value:

JSValue document = 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>");

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];

.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