// web.InvokeScript("window.οnlοad=function(){alert('hello')}");
dom.parentWindow.execScript("window.onload = function () { window.external.WebClick(this); }"); //向页面注入javascript语句
其中window.external.WebClick(this)实现网页javascript调用后台代码
[ComVisible(true)]
public class HtmlBridge
{
public void WebClick(object source)
{
HTMLWindow2 htmlWindow = (HTMLWindow2)source;
//MessageBox.Show("Received: " + source);
foreach (IHTMLElement archor in htmlWindow.document.links)
{
archor.setAttribute("target", "_self");
}
//将所有的FORM的提交目标,指向本窗体
foreach (IHTMLElement form in htmlWindow.document.forms)
{
form.setAttribute("target", "_self");
}
}
}
关联过程
web.ObjectForScripting = new HtmlBridge();
其中web为webbrowser实例