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)]
public object? ObjectForScripting { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ObjectForScripting : obj with get, set
Public Property ObjectForScripting As 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 控制項之應用程式之間的通訊。 此屬性可讓您將動態 HTML (DHTML) 程式碼與您的用戶端應用程式程式碼整合。 針對這個屬性指定的 物件可供網頁腳本作為 window.external 物件使用,這是提供給主機存取的內建 DOM 物件。

您可以將這個屬性設定為您想要其公用屬性和方法可供腳本程式碼使用的任何 COM 可見物件。 您可以使用 將類別標示為 ComVisibleAttribute ,讓類別 COM 可見。

若要從用戶端應用程式程式碼呼叫網頁中定義的函式,請使用 HtmlDocument.InvokeScript 您可以從 屬性擷取 DocumentHtmlDocument 物件的 方法。

適用於

另請參閱