window.parent与window.openner用法与区别
本文章来介绍一下关于window.parent与window.openner用法和区别有需要了解的同学可以参考一下。
frame框架里的页面要改其他同框架下的页面或父框架的页面就用parent
window.opener引用的是window.open打开的页面的父页面。
window.frames对象可以引用iframe里的页面,也可以引用frameset里的页面.
可以这样:
window.frames[0].document.getElementById('xx');
可以这样:
window.frames[0].document.body.innerHTML;
frm = window.parent.window.frames['uploadFrame'];
frmDocument = frm.document;
frm.sb(3); //sb 是uploadFrame页面里的一个函数
对于firefox:
如果你遇到报错:parent.document.frames has no properties
换为如下代码就可以了,这个代码IE,ff兼容. frm = window.parent.window.frames['uploadFrame'];其实 frames 集合并不是挂在 document 而是挂在 window 对象下.
注意这样修改frame里的页面有限制,就是必须是同域下的,否则无法访问
如果是同一域下,但是子域名不同,那么涉及到的js,html文件都加上一句。
document.domain = xxx.com [这里填写你的域名]
document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');
parent.window例子如下:
举例:
a.html 代码如下:
- <html>
- <head><title>父页面</title></head>
- <body>
- <form name="form1" id="form1">
- <input type="text" name="username" id="username"/>
- </form>
- <iframe src="b.html" width=100%></iframe>
- </body>
- </html>
如果我们需要在b.htm中要对a.htm中的username文本框赋值,就如很多上传功能,上传功能页在Ifrmae中,上传成功后把上传后的路径放入父页面的文本框中
我们应该在b.html中写:
- <script type="text/javascript">
- var _parentWin = window.parent ;
- _parentWin.form1.username.value = "xxxx" ;
- </script>
window.opener 是window.open或超链接 打开的子页面调用父页面对象.
例子如下:
a.html 代码如下:
- <html>
- <head>
- <title>父页面</title>
- <script type="text/javascript">
- function openB()
- {
- window.open('b.html','b','width=400,height=200,status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=200,top=100');
- }
- </script>
- </head>
- <body>
- <form id="form1" action="">
- <div>
输入值,代码如下:
- <input type="text" name="username" id="username" /><br />
- <input type="button" value="打开窗口B" onclick="openB();" /><br />
- <a href="b.html" target="_blank">超链接打开B页面</a>
- </div>
- </form>
- </body>
- </html>
b.html 代码如下:
- <html>
- <head>
- <script type="text/javascript">
- function getpValue()
- {
- document.getElementByIdx_x_x_x("span1").innerText=window.opener.document.getElementByIdx_x_x_x("username").value;
- }
- </script>
- </head>
- <body>
- <span>文本框值为:</span><span id="span1"></span><br />
- <input type="button" value="获取父窗口内的文本框值" onclick="getpValue();">
- </body>
- </html>
window.parent 国外的简单用法
- Syntax
- window.parent.frames[num]
- window.parent.frameName
- The parent property contains a reference to the parent window.
- The referencing of these sibling frames can either be done using the frames array, or the name that is assigned by the NAME attribute of the tag.
- var myFrameReference = myWin.parent.frames[2];
简单总结一下:
子页面:
/AAA/AAA.html
父页面:
/BBB/BBB.html
子页面中代码如下
parent.window.location="CCC.html"
结果:
父页面跳转到:
/AAA/CCC.html
opener:对打开当前窗口的window对象的引用,如果当前窗口被用户打开,则它的值为null.
self:自引用属性,是对当前window对象的应用,与window属性同义.
self代表自身窗口,opener代表打开自身的那个窗口,比如窗口A打开窗口B.如果靠window.open方法,则对于窗口B,self代表B自己,而opener代表窗口A.