Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.

The following sample demonstrates how to useJSFunction for invoking methods of a JavaScript object (JSObject).


using DotNetBrowser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JavaScriptObjectsSample
    class Program
        static void Main(string[] args)
            Browser browser = BrowserFactory.Create();

            JSValue value = browser.ExecuteJavaScriptAndReturnValue("document");
            if (value.IsObject() && value is JSObject)
                JSObject document = (JSObject)value;
                // Call document.title = "New Document Title";
                bool success = document.SetProperty("title", JSValue.Create("New Document Title"));
                Console.Out.WriteLine("Title has been updated successfully: " + success);

                // Call document.write("Hello World!");
                JSValue method = document.GetProperty("write");
                if (method.IsFunction() && method is JSFunction)
                    JSFunction documentWrite = (JSFunction)method;
                    documentWrite.Invoke(document, JSValue.Create("Hello World!"));

                JSValue documentContent = browser.ExecuteJavaScriptAndReturnValue("document.body.innerText");
                if (documentContent.IsString())
                    Console.Out.WriteLine("New content: " + documentContent.GetString());


Imports DotNetBrowser

Module Module1
    Sub Main()
        Dim browser As Browser = BrowserFactory.Create()

        Dim value As JSValue = browser.ExecuteJavaScriptAndReturnValue("document")

        If value.IsObject() And TypeOf value is JSObject Then

            Dim document As JSObject = CType(value, JSObject)
            ' Call document.title = "New Document Title";
            Dim success As Boolean = document.SetProperty("title", JSValue.Create("New Document Title"))
            Console.Out.WriteLine("Title has been updated successfully: " + success.ToString())

            ' Call document.write("Hello World!");
            Dim method As JSValue = document.GetProperty("write")

            if method.IsFunction() And TypeOf method is JSFunction Then

                Dim documentWrite As JSFunction = CType(method, JSFunction)
                documentWrite.Invoke(document, JSValue.Create("Hello World!"))
            End If

            Dim documentContent As JSValue = browser.ExecuteJavaScriptAndReturnValue("document.body.innerText")
            if documentContent.IsString() Then

                Console.Out.WriteLine("New content: " + documentContent.GetString())
            End If
        End If

    End Sub
End Module