.NET Framework Class Library
HtmlDocument..::.InvokeScript Method (String)

Executes an Active Scripting function defined in an HTML page.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Function InvokeScript ( _
    scriptName As String _
) As Object
Visual Basic (Usage)
Dim instance As HtmlDocument
Dim scriptName As String
Dim returnValue As Object

returnValue = instance.InvokeScript(scriptName)
C#
public Object InvokeScript(
    string scriptName
)
Visual C++
public:
Object^ InvokeScript(
    String^ scriptName
)
JScript
public function InvokeScript(
    scriptName : String
) : Object

Parameters

scriptName
Type: System..::.String
The name of the script method to invoke.

Return Value

Type: System..::.Object
The object returned by the Active Scripting call.
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.

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>

Visual Basic
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
C#
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");
    }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker