HtmlDocument.InvokeScript 方法

定义

执行在 HTML 页面中定义的活动脚本函数。Executes an Active Scripting function defined in an HTML page.

重载

InvokeScript(String)

执行在 HTML 页面中定义的活动脚本函数。Executes an Active Scripting function defined in an HTML page.

InvokeScript(String, Object[])

执行在 HTML 页面中定义的活动脚本函数。Executes an Active Scripting function defined in an HTML page.

示例

下面的代码示例执行网页中的脚本的内容。The following code example executes the contents of a script in a Web page. 此代码示例要求你已加载以下网页。The code example requires that you have loaded the following Web page.

<HTML>  
<SCRIPT>  
function test(name, address) {  
window.alert("Name is " + name + "; address is " + address);  
}  
</SCRIPT>  
  
<BODY>  
</BODY>  
</HTML>  
  
private void InvokeTestMethod(String name, String address)
{
    if (webBrowser1.Document != null)
    {
        Object[] objArray = new Object[2];
        objArray[0] = (Object)name;
        objArray[1] = (Object)address;
        webBrowser1.Document.InvokeScript("test", objArray);
    }
}
Private Sub InvokeTestMethod(ByVal Name As String, ByVal Address As String)
    If (Not (WebBrowser1.Document Is Nothing)) Then
        Dim ObjArr(2) As Object
        ObjArr(0) = CObj(New String(Name))
        ObjArr(1) = CObj(New String(Address))
        WebBrowser1.Document.InvokeScript("test", ObjArr)
    End If
End Sub

InvokeScript(String)

执行在 HTML 页面中定义的活动脚本函数。Executes an Active Scripting function defined in an HTML page.

public:
 System::Object ^ InvokeScript(System::String ^ scriptName);
public object InvokeScript (string scriptName);
member this.InvokeScript : string -> obj
Public Function InvokeScript (scriptName As String) As Object

参数

scriptName
String

要调用的脚本方法的名称。The name of the script method to invoke.

返回

Object

活动脚本调用所返回的对象。The object returned by the Active Scripting call.

示例

下面的代码示例执行网页中的脚本的内容。The following code example executes the contents of a script in a Web page. 此代码示例要求你在应用程序中有一个名为 WebBrowser1WebBrowser,并且已加载以下网页。The code example requires that you have a WebBrowser in your application called WebBrowser1, and that you have loaded the following Web page.

<HTML>  
  
    <HEAD>  
        <TITLE>Invoke Script Sample</TITLE>  
  
        <SCRIPT>  
            function MyObject() {  
                this.Data = "Data for my private object.";  
            }  
            // Return a string.  
            function test() {  
                return("This is a test.");  
            }  
            // Return a JScript object.  
            function testJScriptObject() {  
                return(new(MyObject));  
            }  
            // Return a DOM element.  
            function testElement() {  
                return(div1);  
            }  
        </SCRIPT>  
    </HEAD>  
  
    <BODY>  
  
        <DIV id="div1">  
        </DIV>  
  
    </BODY>  
  
</HTML>  
  
private void InvokeScript()
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        String str = doc.InvokeScript("test").ToString() ;
        Object jscriptObj = doc.InvokeScript("testJScriptObject");
        Object domOb = doc.InvokeScript("testElement");
    }
}
Private Sub InvokeScript()
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Dim Str As String = .InvokeScript("test")
            Dim JScriptObj As Object = .InvokeScript("testJScriptObject")
            Dim DomObj As Object = .InvokeScript("testElement")
        End With
    End If
End Sub

注解

InvokeScript 返回的对象的基础类型将有所不同。The underlying type of the object returned by InvokeScript will vary. 如果调用的活动脚本函数返回标量数据(如字符串或整数),则它将作为字符串返回。If the called Active Scripting function returns scalar data, such as a string or an integer, it will be returned as a string. 如果它返回基于脚本的对象(如使用 JScript 或 VBScript new 运算符创建的对象),则其类型为 ObjectIf it returns a script-based object, such as an object created using JScript or VBScript's new operator, it will be of type Object. (可以通过调用 GetType 并使用 InvokeMember对此类对象进行调用。)如果它返回一个 HTML DOM 元素(例如 DIVTABLE),则它将为 Object类型;如果已向 MSHTML 添加项目引用,则为。但 DLL 会转换为其特定的非托管 DOM 类型。(You can make calls on such objects by calling GetType and using InvokeMember.) If it returns an HTML DOM element, such as a DIV or a TABLE, it will be of type Object; if you have added a project reference to MSHTML.DLL, however, it will be cast to its specific unmanaged DOM type.

您可以调用在用户计算机上安装的任何活动脚本语言(包括 JScript 和 VBScript)编写的任何函数。You may call any function written in any Active Scripting language installed on the user's computer, including JScript and VBScript.

如果用户已在 Internet Explorer 中显式关闭脚本执行,或者如果网页的当前安全配置不允许,则 InvokeScript 将不执行任何操作。The InvokeScript will do nothing if the user has explicitly turned off script execution in Internet Explorer, or if the current security configuration for the Web page does not allow it.

InvokeScript(String, Object[])

执行在 HTML 页面中定义的活动脚本函数。Executes an Active Scripting function defined in an HTML page.

public:
 System::Object ^ InvokeScript(System::String ^ scriptName, cli::array <System::Object ^> ^ args);
public object InvokeScript (string scriptName, object[] args);
member this.InvokeScript : string * obj[] -> obj
Public Function InvokeScript (scriptName As String, args As Object()) As Object

参数

scriptName
String

要调用的脚本方法的名称。The name of the script method to invoke.

args
Object[]

要传递给脚本方法的自变量。The arguments to pass to the script method.

返回

Object

活动脚本调用所返回的对象。The object returned by the Active Scripting call.

示例

下面的代码示例执行网页中的脚本的内容。The following code example executes the contents of a script in a Web page. 此代码示例要求你在应用程序中有一个名为 WebBrowser1WebBrowser,并且已加载以下网页。The code example requires that you have a WebBrowser in your application called WebBrowser1, and that you have loaded the following Web page.

<HTML>  
    <SCRIPT>  
        function test(name, address) {  
            window.alert("Name is " + name + "; address is " + address);  
        }  
    </SCRIPT>  
  
    <BODY>  
    </BODY>  
</HTML>  
  
private void InvokeTestMethod(String name, String address)
{
    if (webBrowser1.Document != null)
    {
        Object[] objArray = new Object[2];
        objArray[0] = (Object)name;
        objArray[1] = (Object)address;
        webBrowser1.Document.InvokeScript("test", objArray);
    }
}
Private Sub InvokeTestMethod(ByVal Name As String, ByVal Address As String)
    If (Not (WebBrowser1.Document Is Nothing)) Then
        Dim ObjArr(2) As Object
        ObjArr(0) = CObj(New String(Name))
        ObjArr(1) = CObj(New String(Address))
        WebBrowser1.Document.InvokeScript("test", ObjArr)
    End If
End Sub

注解

InvokeScript 返回的对象的基础类型将有所不同。The underlying type of the object returned by InvokeScript will vary. 如果调用的活动脚本函数返回标量数据(如字符串或整数),则它将作为字符串返回。If the called Active Scripting function returns scalar data, such as a string or an integer, it will be returned as a string. 如果它返回基于脚本的对象(如使用 JScript 或 VBScript new 运算符创建的对象),则其类型为 ObjectIf it returns a script-based object, such as an object created using JScript or VBScript's new operator, it will be of type Object. (可以通过调用 GetType 并使用 InvokeMember对此类对象进行调用。)如果它返回一个 HTML DOM 元素(例如 DIVTABLE),则它将为 Object类型;如果已向 MSHTML 添加项目引用,则为。但 DLL 会转换为其特定的非托管 DOM 类型。(You can make calls on such objects by calling GetType and using InvokeMember.) If it returns an HTML DOM element, such as a DIV or a TABLE, it will be of type Object; if you have added a project reference to MSHTML.DLL, however, it will be cast to its specific unmanaged DOM type.

您可以调用在用户计算机上安装的任何活动脚本语言(包括 JScript 和 VBScript)编写的任何函数。You may call any function written in any Active Scripting language installed on the user's machine, including JScript and VBScript.

如果用户已在 Internet Explorer 中显式关闭脚本执行,或者如果网页的当前安全配置不允许,则此方法不会执行任何操作。This method will do nothing if the user has explicitly turned off script execution in Internet Explorer, or if the current security configuration for the Web page does not allow it.

适用于