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 控件的应用程序之间的通信。 此属性允许将动态 HTML (DHTML) 代码与客户端应用程序代码集成。 为此属性指定的对象可用于网页脚本作为 window.external 对象,它是为主机访问提供的内置 DOM 对象。

可以将此属性设置为希望其公共属性和方法可用于编写代码的任何 COM 可见对象。 可以通过使用 .. ComVisibleAttribute标记类来使类 COM 可见。

若要从客户端应用程序代码调用网页中定义的函数,请使用HtmlDocument.InvokeScript可从属性中检索Document的对象的方法HtmlDocument

适用于

另请参阅