转自: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. javascript笔记5-BOM

    Javascript应用的平台很多,不仅仅针对Web.在Web中使用Javascript,BOM(browser object model,浏览器对象模型)是核心. BOM提供了很多对象,用于访问浏览 ...

  2. 未来WEB程序员

    作为一名程序员,如果你想在这个领域内继续向前进步或者在当前的经济形势下保持不被炒鱿鱼,那么你就决不应当自满自足,你需要继续学习.近日,著名IT评论员Justin James在他的博客中列出了未来五年程 ...

  3. UIWebView的缓存策略,清除cookie

    缓存策略 NSURLRequestCachePolicy NSURLRequestUseProtocolCachePolicy缓存策略定义在 web 协议实现中,用于请求特定的URL.是默认的URL缓 ...

  4. N的阶乘的长度

    阶乘是乘法 , 乘法的话 , 几位数*几位数的位数 就是 哪两个几位数相加 .  这个可以用log10来解决 , 所以有如下代码 . #include<stdio.h> #include& ...

  5. PHP header()函数

    对header函数,我用得最多的就是跳转页面和设置字符集,其他的功能用得比较少. 一.设置字符集 其实我们用的最多的在在html代码当中的<meta>标签里面设置字符集.格式如下: < ...

  6. SQL备份还原,分离附加

    备份.还原.分离.附加 备份:在要备份的数据库上右键点击任务,在选择备份.在打卡的对话框中根据需要选择.注意:备份过期时间不能为0,否则会马上过期.目标可根据需要放在任何位置.最后,点击确定,备份成功 ...

  7. 转:Web.config配置文件详解(新手必看)

    转:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配置文件 ...

  8. USB电源管理

    在USB总线接口协议中,由于涉及电源供电,因此协议中规定了完整的电源管理方案.通过USB电源管理可以实现USB设备的激活.挂起.空闲和睡眠等,从而降低无效的功率消耗,实现系统电源的有效使用和合理分配. ...

  9. free命令

    最近服务器总是出问题,研究一下free 以M的形式显示: 参数: Swap 是交换区信息, Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当 ...

  10. DISCOVAR de novo

    海宝建议用这个拼接软件 http://www.broadinstitute.org/software/discovar/blog/?page_id=98 DISCOVAR – variant call ...