一、C#调用JS函数
1.JS代码
< script language = "javascript" >
function Hello(msg)
{
alert('我是JS函数,参数是:' + msg);
return;
}
< / script >
2.C#代码调用
public class Form1:Form
{
public Form1()
{
InitializeComponent();
string pathName = Application.StartupPath + "\" + "HTMLPage1.htm";
this.webBrowser1.ObjectForScripting = this;
webBrowser1.Navigate(pathName);
}
public void Test()
{
//调用的函数:
webBrowser1.Document.InvokeScript("Hello", new string[] {"ssss"});
}
}
二、JS调用C#函数
1.C#函数
using System.Security.Permissions;
//注意: 类定义前需要加上下面两行,否则调用失败!当前类可以com组件的形式供外部调用
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisible(true)]
public class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string msg)
{
MessageBox.Show("我是Winform函数,参数是:" + msg);
}
}
2.JS里的调用
< script language = "javascript" >
function InvokeFunc()
{
window.external.ShowMessage("呵呵");
}
< /script >