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

【实例简介】

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

【实例截图】

【核心代码】

execScript方式:

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方式:

其中需要作如下设置

由于visual studio2005 中WebBrowser控件已经实现了IDocHostUIHandler,所以

只要把应用程序属性类的 "com可见"选上.

再给 this.webBrowser1.ObjectForScripting = this; 赋值就行了

:

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 = @"

";

}

}

}

InvokeScript方式:

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("getPwd", new object[] { "18780110000" })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值