WebBrowser.InvokeScript 方法

定義

執行目前所載入文件中定義的指令碼函式。

多載

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 的參考。

指令碼函式不存在。

範例

下列範例示範如何使用 ,從 WPF 應用程式 InvokeScript(String) 呼叫檔中的腳本函式。 在此範例中,腳本函式沒有參數。

以下是實作將從 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 事件來偵測檔何時完成載入。

如果您未將足夠的參數值傳遞至您叫用的腳本,您未傳遞值的參數將會有未定義的值。 如果您傳遞太多參數值,則會忽略多餘的值。

適用於