webbrowser 调用javascript函数的使用一例,环境c#+html

本文介绍如何使用C#中的WinForm WebBrowser控件加载本地HTML文件,并通过三种不同方式实现与HTML页面中的JavaScript函数进行交互,包括直接调用JS函数和修改DOM元素。

这两天写一个程序,winform的webbrowser装载htm文件,然后和winform窗体交互。

1.page.htm文件源代码

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title></title>
    
<script type="text/javascript">
<!--
function F1(obj)
{
        document.all[
"Country"].innerText=obj;
}

function F2()
{
        document.all[
"Country"].innerText="法国"
}

-->
    
</script>
</head>
<body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0">
    
                    
<table cellspacing="0" cellpadding="0" width="94%" border="0">
                        
                        
<tr>
                            
<td style="width: 32px" align="center" bgcolor="#f0f8ff" height="26">
                                
<font color="#ff0000">1</font></td>
                            
<td style="width: 225px" align="center" bgcolor="#f0f8ff" height="26">
                                
<div id="Country">
                                    中国
</div>
                            
</td>
                        
</tr>
                      
</table>
                        
</body>
</html>

2.TestForm源代码

private void TestForm_Load(object sender, EventArgs e)
{
      webBrowser1.Url = new Uri("C:/page.htm");//指定要载入的网页
}

private void ButtonF1(object sender, EventArgs e)
{
        mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
                mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
               win.execScript("F1('日本')", "javascript");//调用函数F1
}

private void ButtonF2(object sender, EventArgs e)
{
        mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
                mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
               win.execScript("F2()", "javascript");//调用函数F2
}

private void ButtonF3(object sender, EventArgs e)
{
        mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
               mshtml.IHTMLElement el = (mshtml.IHTMLElement)win.document.all.item("Country", null);//取得页面上的Country Dom对象
               el.innerText="荷兰";//直接修改页面上Country对象的属性
}

//调用webbrowser的DocumentCompleted事件来自动修改页面属性,修改属性的方法可以用上面的三种方法。我用的是第一种方法
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
                mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
               win.execScript("F1('日本')", "javascript");//调用函数F1
        }

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值