window.opener的用法

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

好比在父窗体parentForm里面 经过 window.open("subForm.html"),那么在subform.html中 window.openerhtml

就表明parentForm,能够经过这种方式设置父窗体的值或者调用js方法。java

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

    2,若是window.opener存在,设置parentForm中stockBox的值。less

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

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

}orm

 

1>window.opener 的用法htm

在通常的用法中,只是用来解决关闭窗口时不提示弹出窗口,   而对它更深层的了解通常比较少。其   实   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.parent.close();

 

 

 

 

经过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();
     }

本文来自CSDN博客:http://blog.csdn.net/zj1103/archive/2009/05/05/4152274.aspx

相关文章
相关标签/搜索