Javscript调用iframe框架页面中函数的方法
本文实例讲述了在Javscript调用iframe框架页面中函数的方法,这个调用方法其实非常的简单,有了这个方法我们就可以实现iframe之间传值或修改值了,操作起来都非常的简单。分享给大家供大家参考。具体实现方法如下:
访问iframe里面的函数:
document.getElementById('commentIframe').contentWindow.hasLogined();commentIframe为iframe的id.
要在window.onload里面执行
例子如下:
1.html
<ahref="#"onclick="window.frames['frame1'].MyNext()">aa</a> <iframeid="frame1"src="2.html"></iframe>
2.html页面
<scriptlanguage="javascript"type="text/javascript">
functionMyNext()
{
alert(1);
}
</script>
在1.htm中点击test按钮,可以使2.htm(iframe页面)中mybutton按钮失效.就这么简单,呵呵.如果要调用2.htm中的JS函数这样写:
self.frames['a'].funtionname(param)
在1.htm中调用2.htm中的JS函数:iframe2.showInfo();
例子说明:
假设有2个页面,index.html和inner.html。其中index.html中有一个iframe,这个iframe的src指向inner.html。
我们现在要做的就是:
1.在index.html中调用inner.html上的一个js方法
2.在inner.html中调用index.html上的一个js方法
实现代码如下:
index.html:
<html>
<head>
<scripttype="text/javascript">
functionff(){
alert(">>thisisindex'sjsfunction index.html");
}
</script>
</head>
<body>
<divstyle="background:lightblue;">
Thisisindexpage.
<inputtype="button"value="runindex'sfunction"onclick="ff();"/>
<inputtype="button"value="runinnerpage'sfunction"onclick='window.frames["childPage"].sonff();'/>
</div>
<iframeid="childPage"name="childPage"src="inner.html"width="100%"frameborder="0"></iframe>
</body>
</html>
inner.html:
<html>
<head>
<scripttype="text/javascript">
functionsonff(){
alert(">>thisisinnerpage'sjsfunction");
}
</script>
</head>
<body>
<divstyle="background:lightgreen;">
Thisisinnerpage.
<inputtype="button"value="runindex'sfunction"onclick='parent.window.ff();'/>
<inputtype="button"value="runinnerpage'sfunction"onclick="sonff();"/>
</div>
</body>
</html>
希望本文所述对大家基于javascript的web程序设计有所帮助。