转自: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. postfix 邮件备份方法

    postfix 邮件备份方法: postfix的bcc(密送)功能可以根据条件,将所有经过postfix队列的邮件根据规则密送到指定的邮箱. postfix带有三个bcc参数: ①.always_bc ...

  2. Minimum Inversion Number_线段树||树状数组

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  3. 微信 关闭手机微信内置浏览器的js

    WeixinJSBridge.call('closeWindow');

  4. 微信OAuth2网页授权

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using YTO.WeiX ...

  5. 小P的图论课 (模拟退火)

    uses math; ; INF=; var n,m,i,x,y,sum,ans,delta:longint; map:..maxn,..maxn] of longint; flag:..maxn] ...

  6. sprint计划会议

    会议召开时间表 日期 时间 内容 05/09 21:00-22:00 讨论题目(未果) 05/10 21:00-21:30 确定题目(网络助手) 05/13 21:00-21:45 讨论软件页面设计 ...

  7. 《JavaScript Ninja》之函数是根基

    函数是根基 理解函数为什么如此重要 JavaScript 是一门 函数式语言 . 函数为什么是第一型对象 在 JavaScript 中,函数可以共处,可以将其视为其他任意类型的 JavaScript ...

  8. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

  9. Core Java Volume I — 3.6. Strings

    3.6. StringsConceptually, Java strings are sequences of Unicode characters(Java的字符串是一个Unicode序列). Fo ...

  10. Wamp安装使用+Git for Windows

    相信很多朋友都曾在windows上做过web开发,我们常用的Web应用程序平台是:Apache+Mysql+Perl/PHP/Python,在windows下集成称为WAMP.web开发新手有时候由于 ...