0 out of 2 rated this helpful - Rate this topic

WebBrowser::InvokeScript Method (String, array<Object>)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
public:
Object^ InvokeScript(
	String^ scriptName, 
	... array<Object^>^ args
)

Parameters

scriptName
Type: System::String
The name of the script function to execute.
args
Type: array<System::Object>
The parameters to pass to the script function.

Return Value

Type: System::Object
The object returned by the Active Scripting call.
ExceptionCondition
ObjectDisposedException

The WebBrowser instance is no longer valid.

InvalidOperationException

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

COMException

The script function does not exist.

InvokeScript(String, array<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.

The following example shows how to call script functions in a document from an application by using InvokeScript(String, array<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("JavaScriptFunctionWithParameters", 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);
    }
  }
}

.NET Framework

Supported in: 4.5, 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.