【实例简介】

涵盖了几种常用的 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脚本的几种方法整理(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.NET CS文件中输出JavaScript脚本的3种方法以及区别

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

  4. 每天一个linux命令(62):sh命令 /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编程===执行shell脚本的四种方法

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

随机推荐

  1. 事关Animation Tree的工作随笔(二)

    上回说到,游戏项目中客观会遇到逻辑状态的复杂性和动画状态的单一性之间的矛盾,那么Animation Tree是如何解决这个问题的呢? 这又需要引入一个定律:就是逻辑状态无论有多么复杂,但一套逻辑状态组 ...

  2. 关于sys、system、sysman等在EM中登录的问题

    1.sysman要先在SQL*Plus上解锁: (1)以"sysdba"的身份登录 conn system/密码 as sysdba; (2)解锁 alter user sysma ...

  3. css3标签

    -moz代表firefox浏览器私有属性 -ms代表ie浏览器私有属性 -webkit代表chrome.safari私有属性 -o代表opera私有属性 border-radius:2em; 向div ...

  4. avalon学习笔记一 列表及条件过滤

    好长时间都没有更新博客了,不是因为没有学习新的东西,而是到了新的单位每天玩命加班实在是太累了!经过一年的努力吧,终于可以轻松一下了.废话少说,直接干货吧! 由于是学习阶段,就直接拿了公司的二级页面做了 ...

  5. mysqlbinlog恢复数据

    操作命令: 复制代码代码如下: show binlog events in 'mysql-bin.000016' limit 10; reset master 删除所有的二进制日志flush logs ...

  6. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...

  7. 使用NSURLCache为NSURLRequest设置缓存

    http://m.blog.csdn.net/blog/u011439689/18734363#

  8. iOS_SN_沙盒文件操作及位置

    转载:http://blog.csdn.net/hello_hwc/article/details/44916909 沙盒的结构如下所示 一 访问Bundle 注意Bundle只读,不能写入 创建一个 ...

  9. c#字符串方法

    作者: 常浩 staticvoid Main(string[] args) { string s =""; //(1)字符访问(下标访问s[i]) s ="ABCD&qu ...

  10. 对话框(alert,prompt,confirm,showModalDialog)

    alert大部分浏览器中会产生阻塞,confirm,prompt都会产生阻塞 关于showModalDialog的介绍:(转自JS中showModalDialog 详细使用) 基本介绍:        ...