window.opener的用法

window.opener 的用法
window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以写为:

window.opener.document.getElementById("name").value = "输入的数据";


对于javascript中的window.opener没有很好的理解。
为什么框架中不能使用,弹出窗口的父窗口不能在框架里面的某个页面呢?那怎样通过弹出窗口操作框架中的父窗口呢?

opener.parent.frames['frameName'].document.all.input1.value 试试这个:)

>window.opener 的用法

在一般的用法中,只是用来解决关闭窗口时不提示弹出窗口, 而对它更深层的了解一般比较少。其 window.opener是指调用window.open方法的窗口。
在工作中主要是用来解决部分提交的。这种跨页操作对工作是非常有帮助的。
如果你在主窗口打开了一个页面,并且希望主窗口刷新就用这个,打开页面的window.opener就相当于
主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虚拟的目录:如struts的*.do会提示你重试

你可以改成这样 window.opener.yourformname.submit()
就好了

2〉

在应用中有这样一个情况,
在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口


function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){
window.opener.location="javascript:reloadPage();";
}
}

</script>
上面的代码在关闭B窗口的时候会提示错误,说缺少Object,正确的代码如下:
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.opener=null;
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){//如果已经执行了closeWin方法,则不执行本方法
window.opener.location="javascript:reloadPage();";
}
}

</script>
reloadPage方法如下:
function reloadPage() {
history.go(0);
document.execCommand("refresh")
document.location = document.location;
document.location.reload();
}
PS:由于需要支持正常关闭和强制关闭窗口时能捕捉到事件,用了全局变量hasClosed

==============================================

补充,在父窗口是frame的时候在刷新父窗口的时候会出现问题:

The page cannot be refreshed without resending the information.
后修改如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
不需要执行自带的reload()方法,注意,不要再画蛇添足加上这一句:

window.opener.parent.document.frames.item('mainFrame').location.reload();

========================================================================================
最后,为了同时支持刷新普通父窗口和frame父窗口,代码如下:
function closeWin() {
hasClosed = true;
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
//window.opener.top.mainFrame.location="javascript:reloadPage();";
//self.opener.frames.mainFrame.location.reload(true);
window.opener = null;
window.close();
}
function window.onbeforeunload(){
if (!hasClosed) {
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
window.opener = null;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值