[转]window.opener用法

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

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

该文章经过本人整理所得,欢迎转载,转载时请加上本文地址;本文基于署名 2.5 中国大陆许可协议发布,请勿演绎或用于商业目的,但是必须保留本文的署名张志涛(包含链接如您有任何疑问或者授权方面的协商,请给我留言

window.opener用法的更多相关文章

  1. [转]window.opener用法

    window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...

  2. js jquery 关闭弹出页面 并刷新父页面(window.opener)

    function Closepage() { if (window.opener && !window.opener.closed) { window.parent.opener.lo ...

  3. window.opener调用父窗体方法的用法

    应用实例:        function BindWindowCloss() {            $(window).bind('beforeunload', function () {    ...

  4. javascript window.opener的用法分析

    window.opener 返回的是创建当前窗口的那个窗口的引用 window.opener 的用法 window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接 ...

  5. window.opener的用法

    window.opener 主要用来打开窗体的父窗体,可以通过这种方式设置父窗体的值或者调用js方法. 例如: 1,window.opener.test(); ---调用父窗体中的test()方法 2 ...

  6. window.opener

    window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...

  7. window.onload用法详解:

    网页中的javaScript脚本代码往往需要在文档加载完成后才能够去执行,否则可能导致无法获取对象的情况,为了避免这种情况的发生,可以使用以下两种方式: 一.将脚本代码放在网页的底端,这样在运行脚本代 ...

  8. window.parent 与 window.opener

    window.parent针对iframe,window.opener针对window.open 父页面parent.jsp: <%@ page language="java" ...

  9. window.opener强大功能

    window.opener后面的方法可以调用任意父窗口里面js的方法. eg.query()是父窗口的 function refreshParent(){   window.opener.query( ...

随机推荐

  1. fseek ftell rewind

    下面几个函数的头文件 : <stdio.h>   fseek int fseek( FILE *stream, long offset, int origin ); 第一个参数stream ...

  2. 7 libjpeg使用

    一.交叉编译libjepg编译 tar xzf libjpeg-turbo-1.2.1.tar.gz ./configure –help ./configure --prefix=/work/proj ...

  3. 【STL】-function object

    // Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...

  4. wp8.1 C#技巧: 计时器

    public MainPage() { this.InitializeComponent(); this.timer = new DispatcherTimer();//新建委托时间实例 timer. ...

  5. 操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null

    解决方式: 1.jdbc URL链接为:jdbc.url=jdbc:db2://(ip):50000/(数据库名称):driverType=4;fullyMaterializeLobData=true ...

  6. 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  7. 立体透视 perspective transform-style 倾斜旋转

    1.perspective 是设置镜头距离,距离越远视图越小,视图越近,视图越大.就像相机焦距一样.其只对子元素产生效果. 2.transform-style: preserve-3d 设置3d效果, ...

  8. linux中/etc/fstab文件删除或修改了,导致系统无法启动

    在linux中,/etc/fstab文件是磁盘挂载的问题,若该文件不小心给修改了,或者被删除了,那么就会导致系统无法重启.因为/etc/fstab文件是记录磁盘挂载的信息,若该文件出现了问题,那么对应 ...

  9. 乌龟棋(noip2010)

    分析:该题是经典的动态规划题目. 题目中涉及到卡片数.卡片分4类.格子数等若干信息,又每张卡片仅能使用一次.求到达终点最多能能获得多少分. 从题目中可知卡片的使用顺序影响最终得分,我们可知状态转移和使 ...

  10. objectarx 卸载加载arx模块

    通常情况下,加载卸载arx模块是使用 APPLOAD命令 使用ObjectARX 代码操作,也非常简单,使用2个全局函数即可,参数为名字有扩展名 C++ int acedArxLoad( const ...