在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
【实例简介】
涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码
【实例截图】
【核心代码】
execScript方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
using mshtml; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WebBrowser_Script { public partial class execScriptForm : Form { public execScriptForm() { InitializeComponent(); } private void btnOpen_Click( object sender, EventArgs e) { this .webBrowser1.Navigate( this .txtUrl.Text); } private void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e) { IHTMLDocument2 Doc2 = (IHTMLDocument2)webBrowser1.Document.DomDocument; if (Doc2.parentWindow != null ) { string order = " alert('这里可以执行页面中存在的任意函数' document.body.innerHTML); " ; //MessageBox.Show(order); Doc2.parentWindow.execScript(order, "JavaScript" ); } } } } |
NavigateScript方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace WebBrowser_Script { [ComVisible( true )] public partial class NavigateScriptForm : Form { public NavigateScriptForm() { InitializeComponent(); } private void btnOpen_Click( object sender, EventArgs e) { this .webBrowser1.Navigate( this .txtUrl.Text); } private void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Navigate( @"javascript: function alert(str) { window.external.alertMessage(str); }" ); webBrowser1.ObjectForScripting = this ; } public void alertMessage( string s) { MessageBox.Show(s, "这是自定义的title,呵呵呵" ); } private void NavigateScriptForm_Load( object sender, EventArgs e) { webBrowser1.DocumentText = @" <html> <input type=""button"" value=""测试"" onclick=""alert('好例子网 路过');""> </html> " ; } } } |
InvokeScript方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WebBrowser_Script { public partial class InvokeScriptForm : Form { public InvokeScriptForm() { InitializeComponent(); this .webBrowser1.Navigate( this .txtUrl.Text); } private void btnOpen_Click( object sender, EventArgs e) { var input = this .webBrowser1.Document.GetElementById( "kw" ); input.SetAttribute( "value" , "好例子网" ); var button = this .webBrowser1.Document.GetElementById( "su" ); button.InvokeMember( "click" ); } private void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e) { } } } |
另外:InvokeScript 还可以带参数的形式执自定义行脚本方法
例如: webBrowser1.Document.InvokeScript(" })
标签: WebBrowser
Javascript
实例下载地址
在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码的更多相关文章
- Linux中执行shell脚本的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- Linux中执行shell脚本的4种方法
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...
- 每天一个linux命令(62):sh命令 /Linux中执行shell脚本的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
- 在 Ruby 中执行 Shell 命令的 6 种方法
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...
- 执行shell脚本的几种方法及区别
执行shell脚本的几种方法及区别 http://blog.csdn.net/lanxinju/article/details/6032368 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...
- Python中执行系统命令常见的几种方法--转载
Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...
- 在Oracle中执行动态SQL的几种方法
转载:在Oracle中执行动态SQL的几种方法 以下为内容留存: 在Oracle中执行动态SQL的几种方法 在一般的sql操作中,sql语句基本上都是固定的,如:SELECT t.empno,t.en ...
- shell编程===执行shell脚本的四种方法
使用vim创建一个shell文件,命名 hello.sh #!/bin/bash echo "hello shell !" 在linux中进行加载 chmod +x ./hello ...
随机推荐
- hdu 3917 最大重量封闭图
/*最大重量封闭图: 意甲冠军:一些城市要建路需要负责一些公司,每家公司都需要缴纳个税.该公司将需要花费每路,另一个限制条件,如果那家公司a既定a-b.公司b既定b-c然后选择 公司a 你必须选择一个 ...
- js jsp 时间 日期 控件 插件 简单 实用
js时间控件一般都是找网上的用,这东西平常很少涉及到,一用到找起来却烦死人,不是没用就是太复杂,今天向大家推荐一个简单实用的控件,该控件在不断更新,而且有专门的网站对它进行维护,所以值得一看. 先说它 ...
- Qt使用QGraphicsView实现滑动窗体效果
QGraphicsView用来显示一个滚动视图区的QGraphicsScene内容.QGraphicsScene提供了QGraphicsItem的容器功能.通常与QGraphicsView一起使用来描 ...
- 2.4 Git 基础 - 撤消操作
2.4 Git 基础 - 撤消操作 撤消操作 任何时候,你都有可能需要撤消刚才所做的某些操作.接下来,我们会介绍一些基本的撤消操作相关的命令.请注意,有些撤销操作是不可逆的,所以请务必谨慎小心,一旦失 ...
- Katana概述
OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...
- WebApi2官网学习记录--HttpClient Message Handlers
在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...
- FineUI上传控件
文件上传 现在就简单多了,并且也漂亮多了,参考这个示例. 1: <ext:SimpleForm ID="SimpleForm1" BodyPadding="5px& ...
- lnmp、lamp、lnmpa一键安装包(Updated: 2015-10-25)
lnmp.lamp.lnmpa一键安装包(Updated: 2015-10-25) 2014-12-26 Posted by yeho 这个脚本是使用shell编写,为了快速在生产环境上部署lnmp/ ...
- hdu 2438
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- C语言结构体的字节对齐
Test Code: #include <iostream> #include <cstring> using namespace std; struct A{ int a; ...