原文链接:http://fy5388.blog.163.com/blog/static/56499537201012594314130/
webBrowser调用外部js文件和js函数
‘第一种方法:webbrowser动态调用html和js代码,都是动态的:代码示例:
webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(True);
webBrowser1.Document.Write("<HTML>");
webBrowser1.Document.Write("<HEAD>");
webBrowser1.Document.Write("<TITLE>");
webBrowser1.Document.Write("New Document");
webBrowser1.Document.Write("</TITLE>");
webBrowser1.Document.Write("<script type=‘text/javascript‘ language=‘javascript‘>");
webBrowser1.Document.Write("function SayHello() {");
webBrowser1.Document.Write("var bgObj=document.createElement(‘div‘);");
webBrowser1.Document.Write("bgObj.setAttribute(‘id‘,‘bgDiv‘);");
webBrowser1.Document.Write("bgObj.style.color=‘#fff‘;");
webBrowser1.Document.Write("bgObj.style.background=‘blue‘;");
webBrowser1.Document.Write("bgObj.style.border=‘1px solid #333‘;");
webBrowser1.Document.Write("bgObj.innerHTML=‘this is text from script!‘;");
webBrowser1.Document.Write("document.body.appendChild(bgObj);");
webBrowser1.Document.Write(" }");
webBrowser1.Document.Write("</script>");
webBrowser1.Document.Write("</HEAD>");
webBrowser1.Document.Write("<BODY>aaaa");
webBrowser1.Document.Write("</BODY>");
webBrowser1.Document.Write("</HTML>");
/ / webBrowser1.DocumentText + = " <script type=‘text/javascript‘>" + scriptline10 + " </script>";
webBrowser1.Document.InvokeScript("SayHello"); / / 在Body onload = ‘‘ 里面不会调用!
‘第二种方法:webbrowser加载外部html文件和外部js文件,代码示例:
webBrowser1.Navigate(@"c:\test.htm");
‘外部html文件代码:
< !DOCTYPE HTML Public "-//W3C//DTD HTML 4.0 Transitional//EN" >
< html >
< head >
< title >< / title >
< script Type = "text/javascript" src = "JScript1.js" >< / script >
< / head >
< body onload = "addHello()" >
< / body >
< / html >
‘外部js文件代码:
Function addHello(){
var bgObj = Document.createElement("div");
bgObj.setAttribute(‘id‘,‘bgDiv‘);
bgObj.style.border = "1px solid #333";
bgObj.innerHTML = "this is text from js file!";
Document.body.appendChild(bgObj);
}