iframe 父窗口和子窗口相互的调用方法集锦
用iframe、弹出子页面刷新父页面iframe parent.location.reload();弹出子页面 window.opener.location.reload();子窗口刷新父窗口 self.window.opener.locaction.reload();刷新一open()方法打开的窗口 window.opener.location.href = window.opener.location.href刷新以winodw.showModelDialog()方法打开的窗口 window.parent.dialogArguments.document.execCommand('Refresh');
下面以三个页面分别命名为frame.html、top.html、bottom.html为例来具体说明如何做。
frame.html 由上(top.html)下(bottom.html)两个页面组成,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> frame </TITLE>
</HEAD>
<frameset rows="50%,50%">
<frame name=top src="top.html">
<frame name=bottom src="bottom.html">
</frameset>
</HTML>
现在假设top.html (即上面的页面) 有七个button来实现对bottom.html (即下面的页面) 的刷新,可以用以下七种语句,哪个好用自己看着办了。
语句1. window.parent.frames[1].location.reload();
语句2. window.parent.frames.bottom.location.reload();
语句3. window.parent.frames["bottom"].location.reload();
语句4. window.parent.frames.item(1).location.reload();
语句5. window.parent.frames.item('bottom').location.reload();
语句6. window.parent.bottom.location.reload();
语句7. window.parent['bottom'].location.reload();
top.html 页面的代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> top.html </TITLE>
</HEAD>
<BODY>
<input type=button value="刷新1" οnclick="window.parent.frames[1].location.reload()"><br>
<input type=button value="刷新2" οnclick="window.parent.frames.bottom.location.reload()"><br>
<input type=button value="刷新3" οnclick="window.parent.frames['bottom'].location.reload()"><br>
<input type=button value="刷新4" οnclick="window.parent.frames.item(1).location.reload()"><br>
<input type=button value="刷新5" οnclick="window.parent.frames.item('bottom').location.reload()"><br>
<input type=button value="刷新6" οnclick="window.parent.bottom.location.reload()"><br>
<input type=button value="刷新7" οnclick="window.parent['bottom'].location.reload()"><br>
</BODY>
</HTML>
下面是bottom.html页面源代码,为了证明下方页面的确被刷新了,在装载完页面弹出一个对话框。
bottom.html 页面的代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> bottom.html </TITLE>
</HEAD>
<BODY οnlοad="alert('我被加载了!')">
<h1>This is the content in bottom.html.</h1>
</BODY>
</HTML>
解释一下:
1.window指代的是当前页面,例如对于此例它指的是top.html页面。
2.parent指的是当前页面的父页面,也就是包含它的框架页面。例如对于此例它指的是framedemo.html。
3.frames是window对象,是一个数组。代表着该框架内所有子页面。
4.item是方法。返回数组里面的元素。
5.如果子页面也是个框架页面,里面还是其它的子页面,那么上面的有些方法可能不行。
附:
Javascript刷新页面的几种方法:
1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)
7 location.replace(location)
8 document.URL=location.href
自动刷新页面的方法:
1.页面自动刷新:把如下代码加入<head>区域中
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面.
2.页面自动跳转:把如下代码加入<head>区域中
<meta http-equiv="refresh" content="20;url=http://www.wyxg.com">
其中20指隔20秒后跳转到http://www.wyxg.com页面
3.页面自动刷新js版
<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()',1000); //指定1秒刷新一次
</script>
ASP.NET如何输出刷新父窗口脚本语句
1. this.response.write("<script>opener.location.reload();</script>");
2. this.response.write("<script>opener.window.location.href = opener.window.location.href;</script>");
3. Response.Write("<script language=javascript>opener.window.navigate(''你要刷新的页.asp'');</script>")
JS刷新框架的脚本语句
//如何刷新包含该框架的页面用
<script language=JavaScript>
parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=JavaScript>
self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a> )
//如何刷新另一个框架的页面用
<script language=JavaScript>
parent.另一FrameID.location.reload();
</script>
如果想关闭窗口时刷新或者想开窗时刷新的话,在<body>中调用以下语句即可。
<body οnlοad="opener.location.reload()"> 开窗时刷新
<body onUnload="opener.location.reload()"> 关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>
===================================================================================
一、父窗口调用iframe子窗口方法
1、HTML语法:<iframe name="myFrame" src="child.html"></iframe>
2、父窗口调用子窗口:myFrame.window.functionName();
3、子窗品调用父窗口:parent.functionName();
简单地说,也就是在子窗口中调用的变量或函数前加个parent.就行
4、父窗口页面源码:
- <html>
- <head>
- <script type="text/javascript">
- function say() {
- alert("parent.html------>I'm at parent.html");
- }
- function callChild()
- {
- //document.frames("myFrame").f1();
- myFrame.window.say();
- }
- </script>
- </head>
- <body>
- <input type=button value="调用child.html中的函数say()" onclick="callChild()">
- <iframe name="myFrame" src="child.html"></iframe>
- </body>
- </html>
5、子窗口页面:
- <html>
- <head>
- <script type="text/javascript">
- function say()
- {
- alert("child.html--->I'm at child.html");
- }
- function callParent() {
- parent.say();
- }
- </script>
- </head>
- <body>
- <input type=button value="调用parent.html中的say()函数" onclick="callParent()">
- </body>
- </html>
二、iframe 父窗口和子窗口相互的调用方法
1、IE中使用方法:
父窗口调用子窗口:iframe_ID.iframe_document_object.object_attribute = attribute_value
例子:onClick="iframe_text.myH1.innerText='http://www.pint.com';"
子窗口调用父窗口:parent.parent_document_object.object_attribute = attribute_value
例子:οnclick="parent.myH1.innerText='http://www.pint.com';"
2、Firefox中使用方法:
上面在IE下没有问题,但在firefox下不正常。在firefox下,应该是如下调用方法:
父窗口调用子窗口:window.frames["iframe_ID"].document.getElementById("iframe_document_object").object_attribute = attribute_value
例: window.frames["iframe_text"].document.getElementById("myH1").innerHTML= "http://hi.wonsoft.cn";
子窗口调用父窗口:parent.document.getElementById("parent_document_object").object_attribute = attribute_value
例: parent.document.getElementById("myH1").innerHTML = "http://wonsoft.cn";
3、完整的例子
test.htm
- <HTML>
- <HEAD>
- <TITLE> Test Page </TITLE>
- <script src="prototype-1.4.0.js"></script>
- <script language="javascript">
- function show()
- {
- window.frames["iframe_text"].document.getElementById("myH1").innerHTML = "http://hi.wonsoft.cn";
- }
- </script>
- </HEAD>
- <BODY>
- <iframe height="350" width="600" src="iframe_test.htm" name="iframe_text"></iframe>
- <form action="" method="post">
- <input name="haha" id="haha" type="text" maxlength="30" value="haha" />
- <br />
- <textarea cols="50" rows="5" id="getAttributeMethod"></textarea>
- <input type="button" onClick="show();" value="提交"/>
- </form>
- <h1 id="myH1">d</h1>
- </BODY>
- </HTML>
frame_test.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>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- </head>
- <script language="javascript">
- function show()
- {
- parent.document.getElementById("myH1").innerHTML = http://wonsoft.cn;
- }
- </script>
- <body>
- <h1 id="myH1">ha</h1>
- <form action="" method="post">
- <input name="abc" id="abc" type="text" maxlength="30" value="abc" />
- <br />
- <textarea cols="50" rows="10" id="text"></textarea>
- <br />
- <input type="button" value="提交" onclick="show();"/>
- </form>
- </body>
- </html>
test.htm里面firefox下访问iframe 必须用name,不能用id,所以要改为name="iframe_test" 。(http://chenling1018.blog.163.com/blog/static/1480254200811891041694/)
三、在c#中如何动态改变iframe的src值,动态指向一个网页
1)如果是javascript脚本
给iframe加一个ID如<iframe id=frmList……
在脚本写
frmList.document.location=strNewUrl
2)如果是后台程序
给iframe加一个ID,再加上runat=server 如<iframe id=frmList runat=server ……
在程序里写
frmList.Attributes.Add("src",strNewUrl);
===============================================================================
关于iframe中进行DOM操作的问题,我说只要是不存在跨域的问题,是可以进行iframe 之间的DOM操作的。并且拿了以前写的两篇文章给新同事进行参考——《用document.domain+iframe实现Ajax跨子域》、《于Ajax在浏览器中产生前进后退的实现方法》。其中关于使用iframe进行跨域的demo,由于更换了新域名js8.in,导致了demo不能使用了,昨天紧急修改了一下,总算是可以使用啦~窃喜~
今天就写个完整的使用JavaScript进行iframe DOM操作的文章啦~跟大家讨论下IE和Firefox下iframe DOM操作的差异,然后写一个父窗口操作子窗口DOM,子窗口操作父窗口DOM,子窗口操作子窗口DOM的实例。经测试,断桥残雪写的iframe DOM操作实例,不仅仅只是在IE和Firefox下使用正常,而是可以兼容所有浏览器的。本文只是借IE和Firefox在iframe DOM操作的不同,进行分析说明JavaScript在DOM操作中两者的不同。
IE和Firefox对iframe document对象的差异性
在IE6、IE7中,我们可以使用 document.frames[ID].document 来访问iframe子窗口中的document对象,可是这是不符合W3C标准的写法,也是IE下独有的方法,在Firefox下却不可以使用,Firefox下使用的是符合W3C标准的document.getElementById(ID).contentDocument 方法,今天我在写实例的时候,通过IE8进行测试,IE8也是使用的符合W3C标准的 document.getElementById(ID).contentDocument 方法。所以我们可以写一个在IE和Firefox下通用的获取iframe document对象的函数—getIFrameDOM:
function getIFrameDOM(id){ return document.getElementById(id).contentDocument || document.frames[id].document; }
P.S.:如果我们要获取iframe的window对象,而不是document对象,可以使用document.getElementById(ID).contentWindow的方法。这样我们就可以使用子窗口中的window对象了,比如子窗口中的函数。
在子窗口使用父窗口的函数,获取父窗口document对象
在子窗口中,我们可以通过parent就可以获得父窗口的window对象,如果假如我们在父窗口有一个函数为getIFrameDOM,我们可以通过parent.getIFrameDOM来调用,同理我们使用parent.document就可以在子窗口中访问父窗口的document对象了。
使用JavaScript进行iframe的DOM操作实例
首先,我们在父窗口中引入两个iframe子窗口,ID分别为wIframeA、wIframeB,地址分别为:a.html、b.html。
父窗口主要HTML代码如下:
<div id="pHello" style="margin:10px auto;width:360px;height:30px;">此处可通过iframeB的JavaScript函数,来替换哦~</div> <iframe id="wIframeA" name="myiframeA" src="a.html" scrolling="no" frameborder="0"></iframe> <iframe id="wIframeB" name="myiframeB" src="b.html" scrolling="no" frameborder="0"></iframe>
在子窗口A、B中我放了一个ID为hello的P,以方便我们进行DOM操作演示,子窗口A、B的主要HTML代码如下:
<p id="hello">Hello World!</p>
1、在iframe中,父窗口操作子窗口的DOM
建好了父窗口跟子窗口,这样我们可以在父窗口中,通过以下iframeA()函数来把子窗口A更换P的背景颜色为红色:
function iframeA(){//给子窗口A改变ID为hello的背景色 var a = getIFrameDOM("wIframeA"); a.getElementById('hello').style.background = "red"; } function getIFrameDOM(id){//兼容IE、Firefox的iframe DOM获取函数 return document.getElementById(id).contentDocument || document.frames[id].document; }
2、在iframe中,子窗口操作父窗口的DOM
在子窗口中,我们可以方便的进行父窗口DOM操作,只需要在DOM操作之前添加亦歌parent对象的方法就可以啦,如:在上面的子窗口B中,我们可以使用下面的代码把,父窗口中ID为“pHello”的内容给替换掉:
parent.document.getElementById("pHello").innerHTML="<p style='background:#000;color:#fff;font-size:15px;'>O(∩_∩)O哈哈~用子窗口B就可以替换你!不服吗?</p>";
3、在iframe中,子窗口A操作子窗口B的DOM
既然子窗口可以操作父窗口的window对象和document对象,那么子窗口也可以操作另外的子窗口的DOM啦~断桥残雪在子窗口B中可以直接使用parent直接调用父窗口中的getIFrameDOM函数获得子窗口A的document对象,这样要修改子窗口A的内容就很简单啦,如以下的代码:
var a=parent.getIFrameDOM("wIframeA"); a.getElementById('hello').innerHTML="<span style='color:blue;font-size:18px;background:yellow;'>看,俺子窗口B也能DOM你A!</span>";
完整的实例演示
点击下面地址就可以看到断桥残雪写的完整的iframe DOM操作的演示啦:
JS进行iframe的DOM操作演示点击查看
到这里,你还会以为iframe的DOM操作很难吗?嘿嘿~我想有了父窗口操作子窗口DOM,子窗口操作父窗口DOM,子窗口操作子窗口DOM这三个实例讲解,我们就可以对iframe的DOM操作了如指掌啦~其实iframe在我们开发中还有很多灵活的用法,比如跨域、实现Ajax的前进后退,该兴趣的童鞋可以继续深入的阅读下面两篇文章:用document.domain+iframe实现Ajax跨子域》、《于Ajax在浏览器中产生前进后退的实现方法》
=======================================================================
js
在父窗口中获取iframe中的元素
1、
格式:window.frames["iframe的name值"].document.getElementByIdx_x("iframe中控件的ID").click();
实例:window.frames["ifm"].document.getElementByIdx_x("btnOk").click();
2、
格式:
var obj=document.getElementByIdx_x("iframe的name").contentWindow;
var ifmObj=obj.document.getElementByIdx_x("iframe中控件的ID");
ifmObj.click();
实例:
var obj=document.getElementByIdx_x("ifm").contentWindow;
var ifmObj=obj.document.getElementByIdx_x("btnOk");
ifmObj.click();
在iframe中获取父窗口的元素
格式:window.parent.document.getElementByIdx_x("父窗口的元素ID").click();
实例:window.parent.document.getElementByIdx_x("btnOk").click();
jquery
在父窗口中获取iframe中的元素
1、
格式:$("#iframe的ID").contents().find("#iframe中的控件ID").click();//jquery 方法1
实例:$("#ifm").contents().find("#btnOk").click();//jquery 方法1
2、
格式:$("#iframe中的控件ID",document.frames("frame的name").document).click();//jquery 方法2
实例:$("#btnOk",document.frames("ifm").document).click();//jquery 方法2
在iframe中获取父窗口的元素
格式:$('#父窗口中的元素ID', parent.document).click();
实例:$('#btnOk', parent.document).click();