This documentation描述Web浏览器之间的双向通信控制及其所在的表单,其中包括从浏览器控件中引发事件以由表单处理。
一些关键细节:
在情况下,控制的ObjectForScripting
属性设置为以下形式:
webBrowser1.ObjectForScripting = this;
在浏览器控件中的HTML,window.external
用于访问方法'脚本对象'。
webBrowser1.DocumentText =
"<html><head><script>" +
"function test(message) { alert(message); }" +
"</script></head><body><button " +
"onclick=\"window.external.Test('called from script code')\">" +
"call client code from script code</button>" +
"</body></html>";
因此,这:
调用形式内的Test
方法,传递called from script code
作为参数。
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
在[在WebBrowser中的文档中调用JavaScript中的JavaScript代码](http://stackoverflow.com/questions/3694028/invoke-c-sharp-code-from-javascript-in-a-document-in-a-webbrowser)? –