WebBrowser.InvokeScript Method (String, array<String[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Executes the specified script function, which is defined in the currently loaded HTML, with the specified arguments.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)


Public Function InvokeScript ( _
    scriptName As String, _
    ParamArray args As String() _
) As Object
public Object InvokeScript(
    string scriptName,
    params string[] args


  • scriptName
    Type: System.String
    The name of the script to execute.
  • args
    Type: array<System.String[]
    The arguments to pass to the script function.

Return Value

Type: System.Object
The result of the script invocation.


Exception Condition

The script target is at a cross-domain location.


If the call to InvokeScript loads cross-domain content, you can no longer interact with that content with InvokeScript. For security reasons, you cannot call this method on script targets hosted inside an <iframe>.


In the following example, InvokeScript is called, which in turn calls the LoadSearch function in the following HTML. The HTML file must be hosted in the same domain as the Silverlight application.

<html xmlns="http://www.w3.org/1999/xhtml" >
    <script type="text/javascript" >
    function LoadSearch(searchString) {
        window.location = "http://www.bing.com/search?q=" + searchString
        window.external.notify("Search completed")
Silverlight WebBrowser control.
Partial Public Class MainPage
    Inherits UserControl
    Public Sub New()
    End Sub

    Private Sub WB1_ScriptNotify(ByVal sender As Object, ByVal e As NotifyEventArgs)
        Button1.Content = e.Value

        Button1.IsEnabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim results As Object = WB1.InvokeScript("LoadSearch", New String() {"Silverlight"})
    End Sub
End Class
public partial class MainPage : UserControl
    public MainPage()

    void WB1_ScriptNotify(object sender, NotifyEventArgs e)
        Button1.Content = e.Value;
        Button1.IsEnabled = false;


    private void Button1_Click(object sender, RoutedEventArgs e)
        object results = WB1.InvokeScript("LoadSearch", new string[] { "Silverlight" });
<StackPanel x:Name="LayoutRoot" Height="358" Width="489" Background="LightBlue">
    <WebBrowser  x:Name="WB1" Source="http://localhost/HTMLPage1.htm" Height="272" Width="379" 
                 ScriptNotify="WB1_ScriptNotify" Margin="5" />
    <Button Width="200" x:Name="Button1" Content="Click to search!" Click="Button1_Click" />

Version Information


Supported in: 5, 4


For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.