語言

WebBrowser.InvokeScript 方法

定義

執行目前載入文件中定義的腳本函式。

多載

名稱 Description
InvokeScript(String)

執行由目前載入文件實作的腳本函式。

InvokeScript(String, Object[])

執行目前載入文件中定義的腳本函式。

InvokeScript(String)

執行由目前載入文件實作的腳本函式。

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

參數

scriptName
String

執行腳本函式的名稱。

傳回

該物件由 Active Scripting 呼叫回傳。

例外狀況

該 WebBrowser 實例已不再有效。

無法取得對底層原生的 WebBrowser 參考。

腳本功能不存在。

範例

以下範例展示了如何透過使用 InvokeScript(String),從 WPF 應用程式呼叫文件中的腳本函式。 在這個例子中,script 函數沒有參數。

以下是實作 WPF 中將呼叫的腳本函式的 HTML 文件。

<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>

以下展示 WPF 實作如何呼叫 HTML 文件中的腳本函式。

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);
    }
  }
}

備註

InvokeScript(String) 在實作文件尚未載入完成前,不應被呼叫。 你可以透過處理 LoadCompleted 事件來偵測文件是否完成載入。

適用於

InvokeScript(String, Object[])

執行目前載入文件中定義的腳本函式。

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

參數

scriptName
String

執行腳本函式的名稱。

args
Object[]

這些參數要傳遞給腳本函式。

傳回

該物件由 Active Scripting 呼叫回傳。

屬性

例外狀況

該 WebBrowser 實例已不再有效。

無法取得對底層原生的 WebBrowser 參考。

腳本功能不存在。

範例

以下範例展示了如何透過應用程式 InvokeScript(String, Object[])呼叫文件中的腳本函式。 在此範例中,腳本函式需要參數。

以下是實作將從 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>

以下展示 WPF 實作如何呼叫 HTML 文件中的腳本函式。

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);
    }
  }
}

備註

InvokeScript(String, Object[]) 在實作文件尚未載入完成前,不應被呼叫。 你可以透過處理 LoadCompleted 事件來偵測文件是否完成載入。

如果你沒有傳給你調用的腳本足夠的參數值,那些你沒傳給的參數就會有未定義的值。 如果傳遞過多的參數值,多餘的數值會被忽略。

適用於