我的操作系统版本为Win7,VS版本为VS2012。
之前的一篇博文中提到使用.NET中的WebBrowser控件,调用加载的HTML页面中的JS函数,博文地址在:
http://my.oschina.net/Tsybius2014/blog/643909
不过,最近又遇到了一个棘手的问题,就是如果JS代码在Frame内的HTML页面中,上述博文中调用JS的方法就不灵了。
因此,我今天琢磨了一个新的办法,调用这些JS代码,先贴一组HTML页面的代码:
1、test_page.html
<html>
<head>
<title>Frame测试页面</title>
</head>
<script>
// TODO - 这里输入JS代码
</script>
<frameset cols="40%,60%" rows="2*,*">
<frame name="top_left" src="top_left.html">
<frame name="top_right" src="top_right.html">
<frame name="bottom_left" src="bottom_left.html">
<frame name="bottom_right" src="bottom_right.html">
</frameset>
</html>
2、其下有四个Frame,分居页面的左上、右上、左下、右下四端,以top_left.html为例,代码为:
<html>
<head>
<title>测试子页面</title>
</head>
<body>
<div id="bulletin">你好</div>
<script>
// TODO - 这里输入JS代码
</script>
</body>
</html>
我的Winform工程是用C#语言建立的,窗体结构如下:
代码结构如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WebBrowserFrameTest
{
[System.Runtime.InteropServices.ComVisible(true)]
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
try
{
string path = @"C:\Users\Tsybius\Desktop\测试页面\test_page.html";
webBrowser.Navigate(path);
webBrowser.ObjectForScripting = this;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
我想到了三个方法调用Frame内部的JS函数,现在对它们进行逐一说明:
方法1:给定Frame的name和函数名,调用指定Frame中页面内指定名称的JS函数
C#程序为按钮btnTopLeft1定义单击事件代码:
private void btnTopLeft1_Click(object sender, EventArgs e)
{
webBrowser.Document.InvokeScript("changeBulletin", new object[] { "top_left" });
}
在test_page.html的script标签下写入如下代码:
function changeBulletin(frameName) {
window.frames[frameName].changeBulletin();
}
在top_left.html的script标签下写入如下代码:
function changeBulletin() {
document.getElementById("bulletin").innerHTML = "TOP_LEFT";
}
此时单击按钮btnTopLeft1,就可以看到效果了(文字有变化)。
方法2:以参数的形式给定Frame的name和函数名,调用指定Frame中页面内指定名称的JS函数
方法1的问题很明显,方法1中,主页面的changeBulletin函数是写死的。在实际程序编写中,如果每个JS函数调用,我们都要在父页面实现一个同名的函数,那真是要累死人了。
优化的办法是,为C#程序为按钮btnTopLeft2定义单击事件代码:
private void btnTopLeft2_Click(object sender, EventArgs e)
{
webBrowser.Document.InvokeScript("runJsInFrame", new object[] { "top_left", "changeBulletin" });
}
在test_page.html的script标签下写入如下代码:
function runJsInFrame(frameName, funcName) {
window.frames[frameName][funcName]();
}
这样做达到的效果和方法1是一样的,不过好处是只定义一个函数就好了。
方法3:除以参数形式给定Frame的name和函数名外,还要支持调用函数的参数和返回值
方法2的缺点是,虽然实现了传入Frame的name和函数名灵活调用JS函数,但不能为JS函数添加参数并接收返回值。不能实现这一点,那这个方法就是个残缺的方法,因此我又想了方法3去修复这一问题。
为C#程序为按钮btnTopLeft3定义单击事件代码:
private void btnTopLeft3_Click(object sender, EventArgs e)
{
object obj = webBrowser.Document.InvokeScript("runJsInFrame2", new object[] { "top_left", "addParams", "1", "2", "3" });
if (obj != null)
{
MessageBox.Show(obj.ToString());
}
else
{
MessageBox.Show("null");
}
}
在test_page.html的script标签下写入如下代码:
function runJsInFrame2(frameName, funcName) {
var args = "";
if (arguments.length > 2) {
for (var i = 2; i < arguments.length; i++) {
args += "\"" + arguments[i] + "\"";
if (i < arguments.length - 1) {
args += ",";
}
}
}
var command = "window.frames[\"" + frameName + "\"][\"" + funcName + "\"](" + args + ")";
var result = eval(command);
return result;
}
在top_left.html的script标签下写入如下代码:
function addParams() {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += parseInt(arguments[i]);
}
return sum;
}
这里调用了top_left.html中的addParams函数,将所有的输入参数做加法运算,并返回加法运算的和。
这里使用到了JS的可变参数,runJsInFrame2函数中,传入的第一个参数是Frame的name,传入的第二个参数是函数名,后面任意多个参数可根据实际使用自行添加。
这段代码的运行结果就是,返回一个6(1+2+3=6)。
END