Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
In DotNetBrowser 1.8.2 the JavaScript context events have been added:
ScriptContextCreated
- indicates that JavaScript context has been created.ScriptContextDestroyed
- indicates that JavaScript context has been destroyed.
The ScriptContextCreated
event can be used to execute any JavaScript code before the web page JavaScript is executed:
C#
browser.ScriptContextCreated += (sender, args) => { browser.ExecuteJavaScriptAndReturnValue(args.Context.FrameId, @" screen = new function() { this.width = 100; }"); };
VB.NET
AddHandler browser.ScriptContextCreated, Sub(sender, args) browser.ExecuteJavaScriptAndReturnValue(args.Context.FrameId, " screen = new function() { this.width = 100; }") End Sub
Both events are processed synchronously, blocking JavaScript execution on the web page. It is not recommended to put the long-running code in the corresponding event handler, because this will affect web page loading and displaying performance.