c# webBrowser控件与js的交互
转自:http://blog.csdn.net/sd2131512/article/details/7467564
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]
- 这是为了将该类设置为com可访问
- Url属性:WebBrowser控件显示的网页路径
- ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
- JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。
- // WebBrowser控件显示的网页路径
- webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));
- // 将当前类设置为可由脚本访问
- webBrowser1.ObjectForScripting = this;
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]
- 这是为了将该类设置为com可访问
- Url属性:WebBrowser控件显示的网页路径
- ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
- JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。
- // WebBrowser控件显示的网页路径
- webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));
- // 将当前类设置为可由脚本访问
- webBrowser1.ObjectForScripting = this;
.CS
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Web;
- using System.Security.Permissions;
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaction logic for Window1.xaml
- /// </summary>
- public partial class Window1 : Window
- {
- public Window1()
- {
- InitializeComponent();
- Basic ds = new Basic ();
- webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
- webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
- }
- privatevoid Button_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text = DoSomething.name;
- }
- }
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
- publicclass Basic
- {
- publicstaticstring name;
- publicstring Name
- {
- get { return name; }
- set { name = value; }
- }
- publicvoid ClickEvent(string str)
- {
- this.Name = str;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Web;
- using System.Security.Permissions;
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaction logic for Window1.xaml
- /// </summary>
- public partial class Window1 : Window
- {
- public Window1()
- {
- InitializeComponent();
- Basic ds = new Basic ();
- webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
- webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- textBox1.Text = DoSomething.name;
- }
- }
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
- public class Basic
- {
- public static string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public void ClickEvent(string str)
- {
- this.Name = str;
- }
- }
- }
HTML
- <HTML>
- <head>
- <mce:script language="JavaScript" type="text/javascript"><!--
- function Selec()
- {
- var divV=document.getElementById('div2').innerText;
- window.external.ClickEvent(divV);
- }
- // --></mce:script>
- </head>
- <Body>
- <Form>
- <div id="div1" onClick="Selec();">000000000000</div>
- <div id="div2">111111</div>
- </Form>
- </Body>
- </HTML>
- <HTML>
- <head>
- <mce:script language="JavaScript" type="text/javascript"><!--
- function Selec()
- {
- var divV=document.getElementById('div2').innerText;
- window.external.ClickEvent(divV);
- }
- // --></mce:script>
- </head>
- <Body>
- <Form>
- <div id="div1" onClick="Selec();">000000000000</div>
- <div id="div2">111111</div>
- </Form>
- </Body>
- </HTML>
如果需要在运行时点击按钮后再将值传入页面显示,则用下列方法传值
this.webBrowser1.InvokeScript("js中的函数",“要传的值”);
桂棹兮兰桨,击空明兮溯流光。