Take the 2-minute tour ×

Sorry for stupid question, I just want to pass a JOSN string to the webpage in the webbrowser from my VB/C# code, and can the javascript in webpage call the VB/C# method through some interface ? Just like android javascript interface, many thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If I understand you correctly,

webBrowser2.ObjectForScripting = new ScriptClass();
webBrowser2.DocumentText = "<html><script>window.external.Test('hello')</script></html>";

public class ScriptClass
    public void Test(string msg)
share|improve this answer

So you're using a WebBrowser control and need to call a JavaScript method within the page it shows? Yeah, you can do this. It's not on the WebBrowser itself, but the HtmlDocument object, exposed by the WebBrowser's Document property, has an InvokeScript() method group, which will allow you to programatically invoke any ECMAScript-compliant method within the DOM of the page, optionally passing a series of parameters as an Object array.

That, in turn, can be used to trigger a call from JavaScript to a JSON web service (which you expose in your main CLR program, and told the page about by passing it the info via InvokeScript()). This is basic JSON/AJAX client-server scripting, all wrapped up into a single program talking to itself. It's not the most efficient way to get things done, but if you already have these layers that get the job done, and just want to release a self-contained app, it works.

share|improve this answer

Your Answer


