Uredi

Deli z drugimi prek


HtmlDocument.InvokeScript Method

Definition

Executes an Active Scripting function defined in an HTML page.

Overloads

InvokeScript(String)

Executes an Active Scripting function defined in an HTML page.

InvokeScript(String, Object[])

Executes an Active Scripting function defined in an HTML page.

Examples

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)

Executes an Active Scripting function defined in an HTML page.

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

Parameters

scriptName
String

The name of the script method to invoke.

Returns

The object returned by the Active Scripting call.

Examples

The following code example executes the contents of a script in a Web page. 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

Remarks

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. If it returns a script-based object, such as an object created using JScript or VBScript's new operator, it will be of type Object. (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.

You may call any function written in any Active Scripting language installed on the user's computer, including JScript and VBScript.

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.

Applies to

InvokeScript(String, Object[])

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);
public object? InvokeScript (string scriptName, object[]? args);
member this.InvokeScript : string * obj[] -> obj
Public Function InvokeScript (scriptName As String, args As Object()) As Object

Parameters

scriptName
String

The name of the script method to invoke.

args
Object[]

The arguments to pass to the script method.

Returns

The object returned by the Active Scripting call.

Examples

The following code example executes the contents of a script in a Web page. 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

Remarks

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. If it returns a script-based object, such as an object created using JScript or VBScript's new operator, it will be of type Object. (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.

You may call any function written in any Active Scripting language installed on the user's machine, including JScript and VBScript.

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.

Applies to