WebBrowser.InvokeScript 메서드

정의

현재 로드된 문서에 정의된 스크립팅 함수를 실행합니다.

오버로드

Name 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

실행할 스크립트 함수의 이름입니다.

반품

활성 스크립팅 호출에서 반환된 개체입니다.

예외

인스턴스가 WebBrowser 더 이상 유효하지 않습니다.

기본 네이티브 WebBrowser 에 대한 참조를 검색할 수 없습니다.

스크립트 함수가 없습니다.

예제

다음 예제에서는 InvokeScript(String) 사용하여 WPF 애플리케이션에서 문서의 스크립트 함수를 호출하는 방법을 보여 있습니다. 이 예제에서는 스크립트 함수에 매개 변수가 없습니다.

다음은 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>

다음은 HTML 문서에서 스크립트 함수를 호출하는 WPF 구현을 보여줍니다.

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

스크립트 함수에 전달할 매개 변수입니다.

반품

활성 스크립팅 호출에서 반환된 개체입니다.

특성

예외

인스턴스가 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>

다음은 HTML 문서에서 스크립트 함수를 호출하는 WPF 구현을 보여 줍니다.

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 수 있습니다.

호출하는 스크립트에 충분한 매개 변수 값을 전달하지 않으면 값을 전달하지 않는 매개 변수에 정의되지 않은 값이 포함됩니다. 매개 변수 값을 너무 많이 전달하면 초과 값이 무시됩니다.

적용 대상