Share via


WebBrowser.ObjectForScripting 속성

정의

WebBrowser 컨트롤에 표시되는 웹 페이지에 포함되는 스크립트 코드로 액세스할 수 있는 개체를 가져오거나 설정합니다.

public:
 property System::Object ^ ObjectForScripting { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Browsable(false)]
public object ObjectForScripting { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ObjectForScripting : obj with get, set
Public Property ObjectForScripting As Object

속성 값

Object

스크립트 코드에서 사용할 수 있는 개체입니다.

특성

예외

이 속성을 설정할 때 지정된 값이 비공용 형식의 인스턴스인 경우

또는 이 속성을 설정할 때 지정된 값이 COM에 표시되지 않는 형식의 인스턴스인 경우. 자세한 내용은 IsTypeVisibleFromCom(Type)를 참조하세요.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 ObjectForScripting 속성입니다. 이 예제 ObjectForScripting 에서 속성은 현재 형식으로 설정됩니다.

using System;
using System.Windows.Forms;
using System.Security.Permissions;

[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
    private WebBrowser webBrowser1 = new WebBrowser();
    private Button button1 = new Button();

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    public Form1()
    {
        button1.Text = "call script code from client code";
        button1.Dock = DockStyle.Top;
        button1.Click += new EventHandler(button1_Click);
        webBrowser1.Dock = DockStyle.Fill;
        Controls.Add(webBrowser1);
        Controls.Add(button1);
        Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.AllowWebBrowserDrop = false;
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.WebBrowserShortcutsEnabled = false;
        webBrowser1.ObjectForScripting = this;
        // Uncomment the following line when you are finished debugging.
        //webBrowser1.ScriptErrorsSuppressed = true;

        webBrowser1.DocumentText =
            "<html><head><script>" +
            "function test(message) { alert(message); }" +
            "</script></head><body><button " +
            "onclick=\"window.external.Test('called from script code')\">" +
            "call client code from script code</button>" +
            "</body></html>";
    }

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("test",
            new String[] { "called from client code" });
    }
}
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Inherits Form

    Private webBrowser1 As New WebBrowser()
    Private WithEvents button1 As New Button()

    <STAThread()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub

    Public Sub New()
        button1.Text = "call script code from client code"
        button1.Dock = DockStyle.Top
        webBrowser1.Dock = DockStyle.Fill
        Controls.Add(webBrowser1)
        Controls.Add(button1)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Me.Load

        webBrowser1.AllowWebBrowserDrop = False
        webBrowser1.IsWebBrowserContextMenuEnabled = False
        webBrowser1.WebBrowserShortcutsEnabled = False
        webBrowser1.ObjectForScripting = Me
        ' Uncomment the following line when you are finished debugging.
        'webBrowser1.ScriptErrorsSuppressed = True

        webBrowser1.DocumentText = _
            "<html><head><script>" & _
            "function test(message) { alert(message); }" & _
            "</script></head><body><button " & _
            "onclick=""window.external.Test('called from script code')"" > " & _
            "call client code from script code</button>" & _
            "</body></html>"
    End Sub

    Public Sub Test(ByVal message As String)
        MessageBox.Show(message, "client code")
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click

        webBrowser1.Document.InvokeScript("test", _
            New String() {"called from client code"})

    End Sub

End Class

설명

호스팅되는 웹 페이지 간의 통신을 사용 하도록 설정 하려면이 속성을 사용 합니다 WebBrowser 컨트롤과 포함 된 애플리케이션을 WebBrowser 컨트롤. 이 속성에는 클라이언트 애플리케이션 코드를 사용 하 여 동적 DHTML (HTML) 코드를 통합할 수 있습니다. 이 속성에 대해 지정된 개체는 호스트 액세스를 위해 제공되는 기본 제공 DOM 개체인 개체로 window.external 웹 페이지 스크립트에서 사용할 수 있습니다.

이 속성을 공용 속성 및 메서드를 스크립팅 코드에 사용할 수 있게 하려는 COM 표시 개체로 설정할 수 있습니다. 클래스를 COM으로 표시하여 COM을 표시할 ComVisibleAttribute수 있습니다.

클라이언트 애플리케이션 코드에서 웹 페이지에 정의 된 함수를 호출 하려면 사용 합니다 HtmlDocument.InvokeScript 메서드를 HtmlDocument 개체에서 검색할 수 있습니다를 Document 속성.

적용 대상

추가 정보