[C#][JavaScript]WinForm與WebPage的JavaScript互通(一)
namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"<script>function ShowAlert(alertMessage) {alert (alertMessage);}</script>";
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("ShowAlert", new object[] { "alert message..." });
}
}
}
namespace WindowsFormsApplication3 { [PermissionSet(SecurityAction.Demand, Name = “FullTrust”)] [ComVisible(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;
webBrowser1.DocumentText = @"<script>function ShowAlert(alertMessage) {alert (alertMessage);}
window.external.OnWebPageReady();</script>";
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("ShowAlert", new object[] { "alert message..." });
}
public void OnWebPageReady()
{
webBrowser1.Document.InvokeScript("ShowAlert", new object[] { "WebPage Ready..." });
}
}
}