`
113.com
  • 浏览: 76995 次
  • 来自: 广州
社区版块
存档分类
最新评论

[转]window.opener用法

    博客分类:
  • JS
阅读更多



window.opener 实际上就是通过window.open打开的窗体的父窗体。

比如在父窗体parentForm里面 通过 window.open("subForm.html"),那么在subform.html中 window.opener

就代表parentForm,可以通过这种方式设置父窗体的值或者调用js方法。

如:1,window.opener.test(); ---调用父窗体中的test()方法

    2,如果window.opener存在,设置parentForm中stockBox的值。

    if (window.opener && !window.opener.closed) {

       window.opener.document.parentForm.stockBox.value = symbol;

}



1>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;
         }
     }
关于window.opener

window.opener 的用法

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

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

    对于javascrīpt中的window.opener没有很好的理解。

    为什么框架中不能使用,弹出窗口的父窗口不能在框架里面的某个页面呢?那怎样通过弹出窗口操作框架中的父窗口呢?

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







正确使用window.open返回对象的opener
 


众所周知JavaScript中:

var win = window.open(url,windowName,...); 的使用,

而win.opener则是指向父窗口的引用

然而,有种情况却比较特别,

假如有两个窗口window1和window2

按下列步骤执行:

var win = window.open(url,windowName,...);// (window1)

var win = window.open(url,windowName,...);//(window2)

其中先后这两次打开的子窗口的windowName一样

此时你会发现在window2中的win.opener却不是指向window2的,却是指向window1.

如果你想在子窗口关闭父窗口的话,就不正确了,因此可以修改上面的执行方法为:

var win = window.open(url,windowName,...);? (window1)

win.opener = window;

var win = window.open(url,windowName,...);? (window2)

win.opener = window;

只有这样修改才OK












通过window.showModalDialog或者.showModelessDialog弹出的页面

这种情况需要两个步骤:
1 在父窗口.showModalDialog或.showModelessDialog方法的第二个参数传递window对象
比如: window.showModelessDialog('a.htm',window);
2 在a.htm中就可以通过window.dialogArguments获取该参数
比如: window.dialogArguments.fun1();
PS:子窗口可以通过设置window.returnValue设置页面返回值
比如: window.returnValue=’OK’;window.close();

strRtn=window.showModalDialog(......)

这时,strRtn='ok'



页面中实现:
父页面
function reloadPage() {
         document.form1.submit();
     }
弹出页面调用closeWin();
function closeWin(){
         hasClosed = true;
         window.opener.location="javascript:reloadPage();";
         window.opener=null;
         window.close();
     }
分享到:
评论

相关推荐

    javascript window.opener的用法分析

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

    window.opener用法和用途实例介绍

    window.opener,是通过window.open打开子窗体的父窗体的引用。 比如在父窗体parentForm里面,通过window.open(“subForm.html”),那么在subform.html中window.opener就代表parentForm。既然在子窗体中能够拿到父窗体...

    [removed].reload 刷新使用分析(去对话框)

    使用[removed].reload;刷新时,如果提交数据的动作,则会出现讨厌的对话框! 解决此问题,应该这样写: [removed].href=[removed].href; [removed].reload; 同理,如果是刷新父窗口,应该这样写: window.opener...

    深入学习JavaScript中的bom

    BOM(Broswer Object Model) 凡是 window 的属性和方法,均可以省略“window.” ...2.在子窗口中使用,表示父窗口的window对象 window.opener window.opener.fatherSayHello(); 调用父窗口的方法 window.opener.a

    react-new-window:using使用`window.open`在React中弹出新窗口

    为React 16构建(使用ReactDOM.createPortal )。 阻止弹出窗口的处理程序(通过onBlock prop)。 根据父窗口或屏幕使弹出窗口居中。 安装 npm i react-new-window --save 用法 import React from 'react' import...

    javascript常用对象梳理

    熟练掌握window对象的status、location、name、self、opener属性的使用 Window对象是客户端javascript最高层对象之一,只要打开浏览器窗口,不管该窗口中是否有打开的网页,当遇到BODY、FRAMESET或FRAME元素时,...

    js弹出窗口返回值

    window.opener 的用法在一般的用法中,只是用来解决封闭窗口时不提示弹出窗口, 而对它更深层的懂得一般斗劲少。其 实 window.opener是指调用window.open办法的窗口。 在工作中主如果用来解决项目组提交的。这种...

    107个常用javascript语句

    72.JS中指定当前打开窗口的父窗口:window.opener,支持opener.opener...的多重继续. 73.JS中的self指的是当前的窗口 74.JS中状態栏显示內容:window.status="內容" 75.JS中的top指的是框架集中最顶层的框架 76.JS中...

    新浪网易的评论块制作源码

    新浪网易的评论块制作源码 技术要点: 1.因为 textarea里面不能放图片,所以和新浪的做法一样,选用iframe放内容,然后隐藏一个 textarea...window.opener.XXX(xxx); 运行项目执行 HTMLPage2.htm 页面,进行测试吧

    js setTimeout opener的用法示例详解

    } setTimeout : 延迟多长时间执行什么方法,具体使用://www.jb51.net/article/35535.htm opener: 指parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent。...

    JS中表单的使用小结

    使用window.open()弹出的弹出窗口,刷新父窗口 window.opener.location.reload() 使用window.showDialog弹出的模式窗口 window.dialogArguments.location.reload(); 2.javascript弹出窗口的两种实现方式 —下面给...

    JS中showModalDialog关闭子窗口刷新主窗口用法详解

    网上找了好长时间 大都是window.opener.location.reload(),等等 都不是我想要的 最后终于发现了一个 想知道的就往下看看吧 showModalDialog和showModelessDialog 一、showModalDialog和showModelessDialog有什么不同...

    javascript showModalDialog,open取得父窗口的方法

    通常使用window.open的方式开启新窗口的话 要取得父窗口的控件,可以用window.opener来取得父窗口 然而如果使用showModalDialog的话…却无效 如果有需要的话,需要修改开启的语法以及showModalDialog中的语法 开启...

    javascript函数的解释

    72.JS中指定当前打开窗口的父窗口:window.opener,支持opener.opener...的多重继续. 73.JS中的self指的是当前的窗口 74.JS中状态栏显示内容:window.status="内容" 75.JS中的top指的是框架集中最顶层的框架 76.JS中...

    JS模态窗口返回值兼容问题的完美解决方法

    因系统要兼容原IE已使用的关闭方法,经调试测得,需对window.dialogArguments进行再较验,不然易出问题。 function OKEnd(vals) { if (vals == null) vals = TRUE; if (typeof (window.opener) == undefined) { ...

    JavaScript 定时器 SetTimeout之定时刷新窗口和关闭窗口(代码超简单)

    废话不多说了,直接给大家贴代码了。...使用定时器实现JavaScript的延期执行或重复执行 window对象提供了两个方法来实现定时器的效果,分别是 window.setTimeout()和window.setInterval。其中前者可以使一段

    js关闭子窗体刷新父窗体实现方法

    代码如下: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/–&gt;使用open方式打开的窗体 //使用地址... 使用showModalDialog方法 window.returnValue = ‘refresh’;

    不提示直接关闭网页窗口的JS示例代码

    在IE7、IE8中,使用JavaScript提供的close()方法都可以关闭当前窗口或标签,但都提示讨厌的对话框,找了下代码,终于可以无提示直接关闭了。 JavaScript代码 代码如下:function CloseWin() { window.opener=null;...

Global site tag (gtag.js) - Google Analytics