关于webbrowser如何获取js变量值?
通过InvokeScript方法可以获取js函数的返回值
可是如何才能简单获取js变量的值呢?
VB中可以str=Document.script变量名获取
在.net中就没有类似方法了么?
真希望3.0以后能够加入
还有execScript函数最好也能放在webbrowser的方法中,不必通过mshtml
[解决办法]
参考如下代码:
//项目中添加Micrsoft.mshtml引用//Begin temp.htm-------<html><head><title>demo</title><script language="JavaScript" type="text/javascript">var testText = "Zswang";function ShowMessage(AText){ alert(testText); alert(AText);}</script></head></html>//End temp.htm-------using mshtml;using System.Reflection;private void button1_Click(object sender, EventArgs e){ IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow; Type vWindowType = vWindow.GetType(); object testText = vWindowType.InvokeMember("testText", BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取 Console.WriteLine(testText); vWindowType.InvokeMember("testText", BindingFlags.SetProperty, null, vWindow, new object[] { "Zswang 路过" }); // 设置 vWindowType.InvokeMember("ShowMessage", BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法}private void button2_Click(object sender, EventArgs e){ IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow; vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本}