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;
[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;
- }
- }
- }
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>
<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的交互的更多相关文章
- VB.NET让webbrowser控件中JS脚本错误最新方法(2013-09-16)
最近也是在项目中遇到了webbrowser控件中想关闭JS脚本错误窗口的问题,所以经过多次测试,终于用一段高效实用的代码完美解决webbrowser控件中JS脚本错误窗口关闭的问题. 通过创建一个子线 ...
- 在WebBrowser控件使用js调用C#方法
有时我们需要在WebBrowser控件中嵌入了网页,然后通过html页面调用后台方法,如何实现呢?其实很简单,主要有三步: 在被调用方法所属的类上加上[ComVisible(true)]标签,意思就是 ...
- c#: WebBrowser控件注入js代码的三种方案
聊做备忘. 假设js代码为: string jsCode = @"function showAlert(s) {{ alert('hello, world! ' + s);}}; showA ...
- winform WebBrowser控件中,cs后台代码执行动态生成的js
很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...
- C#通过webbrowser控件与javascript交互
1.C#里调用控件里面网页的js函数 //调用JavaScript的messageBox方法,并传入参数 object[] objects = new object[1]; o ...
- webbrowser 控件实现WinForm与WebForm交互
WebBrowser 控件可以让你装载Windows Form 应用程序中的 Web 网页和其它采用浏览器的文件.可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Fo ...
- 浏览器自动化的一些体会5 webBrowser控件之winform和webBrowser的交互
从winform访问webBrowser,大致就是利用webBrowser提供的解析dom的方法以及用InvokeScript方法执行javascript.这个相对比较简单. 从webBrowser访 ...
- WebBrowser控件使用技巧分享
原文:WebBrowser控件使用技巧分享 在发布“淘宝登货员”时发现不少朋友对WebBrowser控件比较感兴趣,故在此分享一下使用心得. 首先分享一个WebBrowser的扩展类(此类所需的dll ...
- WebBrowser控件的简单应用2
原文:WebBrowser控件的简单应用2 第一个简单应用里面讲述的是如何模拟调用当前网页的元素的事件或者赋值/取值.这次的应用讲述的是1:如何处理弹出新页面的事件(总是在我的浏览器里面现实新页面)2 ...
随机推荐
- catch的执行与try的匹配
这里有一段代码: public class EmbededFinally { public static void main(String args[]) { int result; try { Sy ...
- 2016/7/30 div位置设置,表格大小统一
在做作业过程中遇到的一些问题和我的总结 1.作业中给出了一张背景图,并让我在背景图中一片区域写内容 我的思路是将一个div放在这片区域,那么问题就是如何定义div的位置和大小 查阅资料后发现并不复杂, ...
- [zz]Java中的instanceof关键字
1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...
- Linux怎么使用添加的新硬盘
一.磁盘分区 装过系统后第一块磁盘的设备号是/dev/sda,在你添加一个新的磁盘后一般情况下是/dev/sdb *******进入fdisk界面***** # fdisk /dev/sdbDevic ...
- Java custom annotations
Custom annotation definition is similar as Interface, just with @ in front. Annotation interface its ...
- iOS App提交指南-协议、税务和银行业务
App通过审核时,选择的是手动发布,想着等到自己生日那天来发布,当做留个纪念,结果生日当天发布时,由于App属于收费应用,还需要填写协议.税务和银行信息,结果又急急忙忙地去找了下这方面的资料,现在把整 ...
- JS 日历控件
http://www.cnblogs.com/yank/archive/2008/08/14/1267746.html http://code.google.com/p/lhgcalendar/dow ...
- Matlab 矩阵卷积理解(转载)
转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷 ...
- Sprint第二个冲刺(第四天)
一.Sprint 计划会议: 第四次会议总结情况如下展示:昨天完成了美化按钮.增添图片的功能,今天在我们的努力下又完成了查看用户资料和底栏显示功能,由此可见我们团队的小伙伴都很厉害,也很勤奋.从燃尽图 ...
- WEBRTC源码片段分析(1)音频缓冲拷贝
源码位置webrtc/webrtc/modules/audio_device/ios/audio_device_ios.cc函数OSStatus AudioDeviceIPhone::RecordPr ...