转自:http://blog.csdn.net/sd2131512/article/details/7467564

  1. [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  2. 这是为了将该类设置为com可访问
  3. Url属性:WebBrowser控件显示的网页路径
  4. ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
  5. JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。
  6. // WebBrowser控件显示的网页路径
  7. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));
  8. // 将当前类设置为可由脚本访问
  9. webBrowser1.ObjectForScripting = this;
  1. [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  2. 这是为了将该类设置为com可访问
  3. Url属性:WebBrowser控件显示的网页路径
  4. ObjectForScripting属性:该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
  5. JavaScript通过window.external调用C#公开的方法。即由ObjectForScripting属性设置的类的实例中所包含的公共方法。
  6. // WebBrowser控件显示的网页路径
  7. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/XXX.html", UriKind.RelativeOrAbsolute));
  8. // 将当前类设置为可由脚本访问
  9. 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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Web;
  15. using System.Security.Permissions;
  16. namespace WpfApplication1
  17. {
  18. /// <summary>
  19. /// Interaction logic for Window1.xaml
  20. /// </summary>
  21. public partial class Window1 : Window
  22. {
  23. public Window1()
  24. {
  25. InitializeComponent();
  26. Basic ds = new Basic ();
  27. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
  28. webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
  29. }
  30. privatevoid Button_Click(object sender, RoutedEventArgs e)
  31. {
  32. textBox1.Text = DoSomething.name;
  33. }
  34. }
  35. [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
  36. publicclass Basic
  37. {
  38. publicstaticstring name;
  39. publicstring Name
  40. {
  41. get { return name; }
  42. set { name = value; }
  43. }
  44. publicvoid ClickEvent(string str)
  45. {
  46. this.Name = str;
  47. }
  48. }
  49. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Web;
  15. using System.Security.Permissions;
  16. namespace WpfApplication1
  17. {
  18. /// <summary>
  19. /// Interaction logic for Window1.xaml
  20. /// </summary>
  21. public partial class Window1 : Window
  22. {
  23. public Window1()
  24. {
  25. InitializeComponent();
  26. Basic ds = new Basic ();
  27. webBrowser1.Navigate(new Uri(System.Environment.CurrentDirectory + @"/aaa.html", UriKind.RelativeOrAbsolute));//获取根目录的日历文件
  28. webBrowser1.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问
  29. }
  30. private void Button_Click(object sender, RoutedEventArgs e)
  31. {
  32. textBox1.Text = DoSomething.name;
  33. }
  34. }
  35. [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
  36. public class Basic
  37. {
  38. public static string name;
  39. public string Name
  40. {
  41. get { return name; }
  42. set { name = value; }
  43. }
  44. public void ClickEvent(string str)
  45. {
  46. this.Name = str;
  47. }
  48. }
  49. }

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

  1. <HTML>
  2. <head>
  3. <mce:script language="JavaScript" type="text/javascript"><!--
  4. function Selec()
  5. {
  6. var divV=document.getElementById('div2').innerText;
  7. window.external.ClickEvent(divV);
  8. }
  9. // --></mce:script>
  10. </head>
  11. <Body>
  12. <Form>
  13. <div id="div1" onClick="Selec();">000000000000</div>
  14. <div id="div2">111111</div>
  15. </Form>
  16. </Body>
  17. </HTML>
  1. <HTML>
  2. <head>
  3. <mce:script language="JavaScript" type="text/javascript"><!--
  4. function Selec()
  5. {
  6. var divV=document.getElementById('div2').innerText;
  7. window.external.ClickEvent(divV);
  8. }
  9. // --></mce:script>
  10. </head>
  11. <Body>
  12. <Form>
  13. <div id="div1" onClick="Selec();">000000000000</div>
  14. <div id="div2">111111</div>
  15. </Form>
  16. </Body>
  17. </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中的函数",“要传的值”);

c# webBrowser控件与js的交互的更多相关文章

  1. VB.NET让webbrowser控件中JS脚本错误最新方法(2013-09-16)

    最近也是在项目中遇到了webbrowser控件中想关闭JS脚本错误窗口的问题,所以经过多次测试,终于用一段高效实用的代码完美解决webbrowser控件中JS脚本错误窗口关闭的问题. 通过创建一个子线 ...

  2. 在WebBrowser控件使用js调用C#方法

    有时我们需要在WebBrowser控件中嵌入了网页,然后通过html页面调用后台方法,如何实现呢?其实很简单,主要有三步: 在被调用方法所属的类上加上[ComVisible(true)]标签,意思就是 ...

  3. c#: WebBrowser控件注入js代码的三种方案

    聊做备忘. 假设js代码为: string jsCode = @"function showAlert(s) {{ alert('hello, world! ' + s);}}; showA ...

  4. winform WebBrowser控件中,cs后台代码执行动态生成的js

    很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...

  5. C#通过webbrowser控件与javascript交互

    1.C#里调用控件里面网页的js函数     //调用JavaScript的messageBox方法,并传入参数     object[] objects = new object[1];     o ...

  6. webbrowser 控件实现WinForm与WebForm交互

    WebBrowser 控件可以让你装载Windows Form 应用程序中的 Web 网页和其它采用浏览器的文件.可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Fo ...

  7. 浏览器自动化的一些体会5 webBrowser控件之winform和webBrowser的交互

    从winform访问webBrowser,大致就是利用webBrowser提供的解析dom的方法以及用InvokeScript方法执行javascript.这个相对比较简单. 从webBrowser访 ...

  8. WebBrowser控件使用技巧分享

    原文:WebBrowser控件使用技巧分享 在发布“淘宝登货员”时发现不少朋友对WebBrowser控件比较感兴趣,故在此分享一下使用心得. 首先分享一个WebBrowser的扩展类(此类所需的dll ...

  9. WebBrowser控件的简单应用2

    原文:WebBrowser控件的简单应用2 第一个简单应用里面讲述的是如何模拟调用当前网页的元素的事件或者赋值/取值.这次的应用讲述的是1:如何处理弹出新页面的事件(总是在我的浏览器里面现实新页面)2 ...

随机推荐

  1. 学会使用Ogitor

    这几天在用Ogre读取Ogitor的场景,遇到了不少问题,在网上也找不到详细的说明,虽然读取Ogitor的场景对很多人来说太简单了,但对一些新手来说就有点难了,我刚开始就觉得是无从下手,因此简单的描述 ...

  2. 使用HackRF+GNU Radio 破解吉普车钥匙信号

    引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...

  3. http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

    http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

  4. 深入学习:Windows下Git入门教程(下)

    声明:由于本人对于Git的学习还处于摸索阶段,对有些概念的理解或许只是我断章取义,有曲解误导的地方还请见谅指正! 一.分支 1.1分支的概念. 对于的分支的理解,我们可以用模块化这个词来解释:在日常工 ...

  5. BZOJ 3163 Eden的新背包问题

    分治背包+单调队列优化. 但是为什么maxn要1w多?...不怎么懂. #include<iostream> #include<cstdio> #include<cstr ...

  6. hbm2ddl

    hbm2ddl工具位于Hibernate核心软件包中,而hbm2java工具位于Hibernate工具包中,因此需要下载Hibernate工具包,文件形式为HibernateTools-X.zip. ...

  7. Android沉浸式(侵入式)标题栏(状态栏)Status(二)

     Android沉浸式(侵入式)标题栏(状态栏)Status(二) 附录1以xml写style实现了Android沉浸式(侵入式)状态栏(标题栏),同样以上层Java代码实现.在附录文章1的基础上 ...

  8. spring 常见错误

    1. 数据库字段和实体字段不匹配,尤其是数据表字段和实体字段的类型不匹配 2. 数据表中日期字段不能为空(sql语句用了聚合函数min或者max),此时数据表中没有数据就会报此类错误.

  9. LCA(倍增)

    type arr=record v,nt:longint; end; ; lx=; ..maxn] of longint; eg:..maxn*] of arr; d:..maxn] of longi ...

  10. linode空间lamp环境的搭建

    安装LAMP的命令如下,请依次执行: apt-get updateapt-get upgrade –show-upgradedapt-get install apache2a2enmod rewrit ...