webBrowser1.Document.InvokeScript("messageBox", objects);
完整代码如下:
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.IO.FileInfo file = new System.IO.FileInfo("index.htm");
// WebBrowser控件显示的网页路径
webBrowser1.Url = new Uri(file.FullName);
// 将当前类设置为可由脚本访问
webBrowser1.ObjectForScripting = this;
}
private void button1_Click(object sender, EventArgs e)
{
// 调用JavaScript的messageBox方法,并传入参数
object[] objects = new object[1];
objects[0] = "C#访问JavaScript脚本";
webBrowser1.Document.InvokeScript("messageBox", objects);
}
// 提供给JavaScript调用的方法
public void MyMessageBox(string message)
{
MessageBox.Show(message);
}
}
Dnew.cn 注:原文:http://www.cnblogs.com/xds/archive/2007/03/02/661838.html










