在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

时间:2022-09-26 10:48:36

【实例简介】

涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码
【实例截图】

在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
【核心代码】
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脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码的更多相关文章

  1. Linux中执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  2. Linux中执行shell脚本的4种方法

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  3. ASP&period;NET CS文件中输出JavaScript脚本的3种方法以及区别

    Response.Write 与   Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...

  4. 每天一个linux命令(62):sh命令 &sol;Linux中执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

  5. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  6. 执行shell脚本的几种方法及区别

    执行shell脚本的几种方法及区别 http://blog.csdn.net/lanxinju/article/details/6032368 (认真看) 注意:如果涉及到脚本之间的调用一定要用 . ...

  7. Python中执行系统命令常见的几种方法--转载

    Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...

  8. 在Oracle中执行动态SQL的几种方法

    转载:在Oracle中执行动态SQL的几种方法 以下为内容留存: 在Oracle中执行动态SQL的几种方法 在一般的sql操作中,sql语句基本上都是固定的,如:SELECT t.empno,t.en ...

  9. shell编程&equals;&equals;&equals;执行shell脚本的四种方法

    使用vim创建一个shell文件,命名 hello.sh #!/bin/bash echo "hello shell !" 在linux中进行加载 chmod +x ./hello ...

随机推荐

  1. 附7 turbine

    一.作用 聚集同一个微服务的相同的commandKey.Threadpool.commandGroupKey数据进行聚合 二.配置 1.集群(cluster)(turbine聚集数据的粒度) turb ...

  2. python检测文件的MD5值

    python检测文件的MD5值MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权 ...

  3. Anchor 对象

    更改一个链接的文本.URL 以及target<html><head><script type="text/javascript"> functi ...

  4. 关于远程连接MySQL数据库的问题解决

    安装MySQL sudo apt-get install mysql-server 这个应该很简单了,而且我觉得大家在安装方面也没什么太大问题,所以也就不多说了,下面我们来讲讲配置. 配置MySQL ...

  5. halt和shutdown 的区别

    1.halt -h 标准情况下是关机 但是要手动关闭电源 .有些发行版增强了halt脚本 使其可以关闭电源 halt执行时﹐杀死应用进程﹐执行sync系统调用﹐文件系统写操作完成后就会停止内核. 2. ...

  6. UVa 673 Parentheses Balance(栈的使用)

     栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description You are ...

  7. 使用iconfont图标

    iconfont.cn基本使用 登录iconfont.cn网站,直接使用github账号即可登录 输入关键字搜索需要的图标,然后在需要的图标上点击'添加入库' 点击网站右上角的购物车图标,查看当前已入 ...

  8. 让公式在网页传播——mathJAX

    让公式在网页传播--mathJAX 对于学生党而言,写公式最好的工具是LaTeX,但LaTeX把公式展示到互联网上就有些困难,而使用截图又不太雅观.幸运的是,mathJAX引擎可以在浏览器中解析渲染数 ...

  9. 主动触发input框的失去焦点事件,阻止输入法跳出

    今天遇到个问题,我在手机做一个选择生日的功能,但是当我点击input框时,事件选择插件和输入法都弹出来了,很丑,然后就想阻止输入法弹出来, 网上一个方法是:在input框的获取焦点事件里,主动触发失去 ...

  10. Eclipse 乱码 解决方案总结(UTF8 -- GBK)

    UTF8 --> GBK;   GBK --> UTF8 eclipse的中文乱码问题,一般不外乎是由操作系统平台编码的不一致导致,如Linux中默认的中文字体编码问UTF8, 而Wind ...