Bewerken

Delen via


WebBrowser.InvokeScript Method

Definition

Executes a scripting function defined in the currently loaded document.

Overloads

InvokeScript(String)

Executes a script function that is implemented by the currently loaded document.

InvokeScript(String, Object[])

Executes a script function that is defined in the currently loaded document.

InvokeScript(String)

Executes a script function that is implemented by the currently loaded document.

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

Parameters

scriptName
String

The name of the script function to execute.

Returns

The object returned by the Active Scripting call.

Exceptions

The WebBrowser instance is no longer valid.

A reference to the underlying native WebBrowser could not be retrieved.

The script function does not exist.

Examples

The following example shows how to call a script function in a document from a WPF application by using InvokeScript(String). In this example, the script function has no parameters.

The following is the HTML document that implements the script function that will be called from WPF.

<html>  
    <head>  
        <script type="text/javascript">  
            // Function Without Parameters  
            function JavaScriptFunctionWithoutParameters()    
            {  
              outputID.innerHTML = "JavaScript function called!";  
            }  
        </script>  
    </head>  
    <body>  
    <div id="outputID" style="color:Red; font-size:16">  
        Hello from HTML document with script!  
    </div>  
    </body>  
</html>  

The following shows the WPF implementation to call the script function in the HTML document.

private void callScriptFunctionNoParamButton_Click(object sender, RoutedEventArgs e)  
{  
  // Make sure the HTML document has loaded before attempting to  
  // invoke script of the document page. You could set loadCompleted  
  // to true when the LoadCompleted event on the WebBrowser fires.  
  if (this.loadCompleted)  
  {  
    try  
    {  
      this.webBrowser.InvokeScript("JavaScriptFunctionWithoutParameters");  
    }  
    catch (Exception ex)  
    {  
      string msg = "Could not call script: " +  
                   ex.Message +  
                  "\n\nPlease click the 'Load HTML Document with Script' button to load.";  
      MessageBox.Show(msg);  
    }  
  }  
}  

Remarks

InvokeScript(String) should not be called before the document that implements it has finished loading. You can detect when a document has finished loading by handling the LoadCompleted event.

Applies to

InvokeScript(String, Object[])

Executes a script function that is defined in the currently loaded document.

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

Parameters

scriptName
String

The name of the script function to execute.

args
Object[]

The parameters to pass to the script function.

Returns

The object returned by the Active Scripting call.

Attributes

Exceptions

The WebBrowser instance is no longer valid.

A reference to the underlying native WebBrowser could not be retrieved.

The script function does not exist.

Examples

The following example shows how to call script functions in a document from an application by using InvokeScript(String, Object[]). In this example, the script functions require parameters.

The following is the document that implements the script functions that will be called from WPF.

<html>  
    <head>  
        <script type="text/javascript">  
            // Function Without Parameters  
            function JavaScriptFunctionWithoutParameters()    
            {  
              outputID.innerHTML = "JavaScript function 'called: " + message + ".";  
            }  
        </script>  
    </head>  
    <body>  
    <div id="outputID" style="color:Red; font-size:16">  
        Hello from HTML document with script!  
    </div>  
    </body>  
</html>  

The following shows the WPF implementation to call the script functions in the HTML document.

private void callScriptFunctionNoParamButton_Click(object sender, RoutedEventArgs e)  
{  
  // Make sure the HTML document has loaded before attempting to  
  // invoke script of the document page. You could set loadCompleted  
  // to true when the LoadCompleted event on the WebBrowser fires.  
  if (this.loadCompleted)  
  {  
    try  
    {  
      this.webBrowser.InvokeScript("JavaScriptFunctionWithoutParameters", this.messageTextBox.Text);  
    }  
    catch (Exception ex)  
    {  
      string msg = "Could not call script: " +  
                   ex.Message +  
                  "\n\nPlease click the 'Load HTML Document with Script' button to load.";  
      MessageBox.Show(msg);  
    }  
  }  
}  

Remarks

InvokeScript(String, Object[]) should not be called before the document that implements it has finished loading. You can detect when a document has finished loading by handling the LoadCompleted event.

If you do not pass enough parameter values to the script that you are invoking, the parameters that you do not pass values to will have the undefined value. If you pass too many parameter values, the excess values are ignored.

Applies to