在C#webBrowser控件中调用Javascript函数

我在C#中使用webBrowser控件来加载网页,需要调用返回字符串值的JavaScript函数。 我有一个使用InvokeScript方法的解决方案,我尝试了很多,但一切都失败了。

你能说明什么失败吗?

我的下面的示例包含一个带有WebBrowser和Button的表单。

最后称为y的对象有句“我做了!”。 所以和我一起工作吧。

public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.DocumentText = @"  hello!"; } private void button1_Click(object sender, EventArgs e) { object y = webBrowser1.Document.InvokeScript("doIt"); } } 

您可以将参数发送到js函数:

 // don't forget this: [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [ComVisible(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.DocumentText = @"  hello!"; } private void button1_Click(object sender, EventArgs e) { // get the retrieved object from js into object y object y = webBrowser1.Document.InvokeScript("doIt", new string[] { "Snir", "Raki", "Gidon"}); } }