window.parent与window.opener的区别与使用

本文详细解释了如何在iframe页面中通过window.parent和window.opener调用父页面对象,以及它们在不同场景下的应用,包括文本框赋值、上传功能及窗口操作等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://my.oschina.net/u/2331760/blog/389768[/url]
document.frames指的是本页面有iframe内置框架,或许有多个,var frm = document.frames;就是获得本页面所有子框架页面的集合,一般在父页面的JS里用,而window.parent.frames指的是父页面所有框架的集合,一般是在子页面的JS里用。

[b]1、window.parent 是iframe页面调用父页面对象[/b]

举例: a.html
<html>
<head><title>A</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.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrmae中,上传成功后把上传后的路径放入父页面的文本框中),我们应该在b.html中写:
<script type=”text/javascript”>
var _parentWin = window.parent;
_parentWin.form1.username.value = “xxxx”;
</script>


[b]2、window.opener 是 window.open 打开的子页面调用父页面对象[/b]

opener:对打开当前窗口的window对象的引用,如果当前窗口被用户打开,则它的值为null。

self代表自身窗口,opener代表打开自身的那个窗口,比如窗口a.html打开窗口b.html。如果靠window.open方法,则对于窗口b.html,self代表b.html自己,而opener代表窗口a.html。

举例:a.html
<input type=”text” name=”username” id=”username”/>
<a onclick=”window.open(this.href,”,’resizable=yes,width=800,height=600,status’); return false” href=”b.html”>B</a>

如果需要在b.html中对a.html中的表单元素赋值,我们应该在b.html中这么写
<a href=”javascript:try{window.opener.document.getElementById(‘username’).contentWindow.
frames[0].document.getElementsByTagName(‘body’)[0].innerHTML+=’xxx‘}catch(e){};window.close();“>插入</a>

在后面用window.close关闭b.html。WindsPhoto 2.7.3 [url]http://www.wilf.cn/post/WindsPhoto.html[/url] 中在文章编辑页面弹出新窗口(图片列表)后,选择插入已上传图片便是如此实现的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值