通过InvokeScript方法可以获取js函数的返回值 
可是如何才能简单获取js变量的值呢? 
VB中可以str=Document.script变量名获取 
在.net中就没有类似方法了么? 
真希望3.0以后能够加入 
还有execScript函数最好也能放在webbrowser的方法中,不必通过mshtml

解决方案 »

  1.   

    参考如下代码:
    //项目中添加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"); // 执行脚本
    }
      

  2.   

    VB中可以str=Document.script变量名获取  ,具体代码是多少啊 ,,555 我其实就想获取网页文本框的内容       <th width="10%"><span class="nNull">*</span>姓名 </th>
          <td><input class="namefield" name="name" type="text" id="name" disabled="disabled" /></td>,网页文本框为灰色不可用,求VB代码