html调用js函数_JS函数和winform函数之间的相互调用

1、写一个简单的html页面,用于输入日志,代码如下:

<html><head>    <meta charset="UTF-8">    <script>        //写日志(C#调用js)        function writeLog(log) {            var para = document.createElement("p");            var text = document.createTextNode(log);            para.appendChild(text);            document.getElementById("logID").appendChild(para);        }        //写日志(js调用C#)        function callC(){            window.external.callC();        }script>head><body>    <div> <button onclick="callC()">js调用C#方法button>div>    <div id="logID" style="height: 100px;width: 300px;">div>body>html>

用浏览器打开,效果如下:

7511f1d8e6593680040fd47a8421955a.png

2、C#项目中新建【WebBrowserUC】组件,组件继承【WebBrowser】

71885f5e85a6488b9b9be3b094c1e0fa.png

//浏览器页面初始化完成public event HandledEventHandler webBrowserInitComplete;//js调用C#方法public event HandledEventHandler jsCallEvent;public WebBrowserUC(){    InitializeComponent();    this.ParentChanged += WebBrowserUC_ParentChanged;    this.ScrollBarsEnabled = false;}/// /// 因没有loaded事件,用ParentChanged代替,进入一次后,需要移除事件,不然重复调用/// /// /// private void WebBrowserUC_ParentChanged(object sender, EventArgs e){    try    {        this.ParentChanged -= WebBrowserUC_ParentChanged;        string urlPath = AppDomain.CurrentDomain.BaseDirectory + "jstest\\test.html";        this.Url = new Uri(urlPath);        this.DocumentCompleted += WebBrowserUC_DocumentCompleted;    }    catch (Exception)    {    }}/// /// html加载完成事件/// /// /// private void WebBrowserUC_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){    this.DocumentCompleted -= WebBrowserUC_DocumentCompleted;    if (webBrowserInitComplete != null)     {        webBrowserInitComplete(null, null);    }}/// /// 写日志(C#调用js)/// public void writeLog(string log){    this.Document.InvokeScript("writeLog", new object[] { log });}/// /// 写日志(js调用C#)/// public void callC(){    if (jsCallEvent != null)    {        jsCallEvent(null, null);    }}

3、新建Form窗体,将WebBrowserUC控件加入左边Panel,右边Panle放一个RichTextBox控件,用于输入js调用C#方法的日志

bfc308f398542fb02e6450bcb31a0c18.png

private WebBrowserUC webBrowserUC;public Form2(){    InitializeComponent();}private void Form2_Load(object sender, EventArgs e){    //添加浏览器控件    webBrowserUC = new WebBrowserUC();    webBrowserUC.Dock = DockStyle.Fill;    //浏览器页面加载完成事件    webBrowserUC.webBrowserInitComplete += webBrowserUC_webBrowserInitComplete;    webBrowserUC.jsCallEvent += webBrowserUC_jsCallEvent;    panel_browser.Controls.Add(webBrowserUC);}/// /// 浏览器页面加载完成/// /// /// private void webBrowserUC_webBrowserInitComplete(object sender, HandledEventArgs e){    richTextBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+":浏览器页面加载完成\n");}/// /// 调用js方法/// /// /// private void btn_js_Click(object sender, EventArgs e){    string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":C#调用js方法成功";    webBrowserUC.writeLog(log);}/// /// js调用C#方法/// /// /// private void webBrowserUC_jsCallEvent(object sender, HandledEventArgs e){    richTextBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":js调用C#方法成功\n");}

4、运行C#程序,C#调用js方法正常,但是js调用C#方法报js错误,错误内容为找不到方法。

b14eae1cf378680d22c1b7bb6462f6e2.png

5、原因是WebBrowserUC必须加上注解[ComVisibleAttribute(true)] ,设定com可见,以便js可调用,还必须在构造函数加入这句代码,设定js的调用对象:this.ObjectForScripting = this; 

d733a5c10132c141d240f7e62debd051.png

1.6、再次运行c#程序,js调用C#方法和C#调用js方法均正常。

1c8ea77fcfe88af374c40499b4f89241.png

至此JS函数和winform函数之间的相互调用功能完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值