WebBrowser.ObjectForScripting プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As WebBrowser Dim value As Object value = instance.ObjectForScripting instance.ObjectForScripting = value
/** @property */ public Object get_ObjectForScripting () /** @property */ public void set_ObjectForScripting (Object value)
public function get ObjectForScripting () : Object public function set ObjectForScripting (value : Object)
スクリプト コードから使用できるようにするオブジェクト。


このプロパティを使用すると、WebBrowser コントロールによってホストされる Web ページと、WebBrowser コントロールを含むアプリケーションとの間で通信できるようになります。このプロパティによって、ダイナミック HTML (DHTML) コードとクライアント アプリケーションのコードを統合できます。このプロパティに指定されたオブジェクトは、Web ページ スクリプトで window.external オブジェクトとして使用できます。これはホスト アクセスのために用意された組み込み DOM オブジェクトです。
スクリプト コードで使用するパブリック プロパティとパブリック メソッドを持つ、COM 参照可能な任意のオブジェクトを、このプロパティに設定できます。特定のクラスを COM から参照できるようにするには、ComVisibleAttribute を指定します。
Web ページで定義されている関数をクライアント アプリケーションのコードから呼び出すには、Document プロパティから取得できる HtmlDocument オブジェクトの HtmlDocument.InvokeScript メソッドを使用します。

ObjectForScripting プロパティを使用する方法を次のコード例に示します。この例では、ObjectForScripting プロパティに現在のフォームを設定しています。
Imports System 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
using System; using System.Windows.Forms; using System.Security.Permissions; [PermissionSet(SecurityAction.Demand, Name="FullTrust")] [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" }); } }


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


WebBrowser クラス
WebBrowser メンバ
System.Windows.Forms 名前空間
WebBrowser.Document プロパティ
HtmlDocument.InvokeScript
Weblioに収録されているすべての辞書からWebBrowser.ObjectForScripting プロパティを検索する場合は、下記のリンクをクリックしてください。

- WebBrowser.ObjectForScripting プロパティのページへのリンク