WebBrower 与Javascript互相调用

 

首先在包含webbrower控件的窗体 类中加上下面的属性

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

 

然后在Frm_Load里面加入

webBrowser1.ObjectForScripting = this;

 

1.webBrower调用JS

这样就可以调用页面中的js函数了,  调用方法如下:

this.webBrowser1.Document.InvokeScript("callSL");

this.webBrowser1.Document.InvokeScript("ZoomTo",new object[]{coords});

 

this.webBrowser1.Document.InvokeScript(“js函数名”,object[]{参数});

 

2.JS调用C#方法

window.external.MyMessageBox(“hello”);   MyMessageBox是C#中的一个公共方法

  // 提供给JavaScript调用的方法
        public void MyMessageBox(string message)
        {
            MessageBox.Show(message);
        }
需要注意的是 为了相互调用都没问题 一定要确保开头的设置
posted @ 2011-08-24 15:47  千禧牛  阅读(668)  评论(0)    收藏  举报