`
liudaoru
  • 浏览: 1573908 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

关于window.opener

阅读更多

如果想获取一个opener的引用,则需要或者指定链接的target,或者使用window.open方法。

http://www.webreference.com/js/tutorial1/opener.html

Referencing the Opener

If you want to associate a link with a specific window, you can use the TARGET attribute of the <A> tag. A browser window doesn't have a name unless you assign it one. When you open a new window via a link with a TARGET attribute, you are assigning it an explicit name:

 

<A HREF="http://www.intel.com/" TARGET="review">Intel</A>

 

The preceding link opens a new window named review. The following link generates the same result via JavaScript:

 

<A HREF="http://www.intel.com/"
   onClick="window.open('http://www.intel.com/', 'review'); return false">Intel</A>

 

As you can see, assigning a name to a new window is very simple. But what about naming an existing window, or a user-generated window?

The name property of the window object lets you set the name of any window via JavaScript. If you want to launch a new window that will need to associate links and forms with the parent (opener) window, be sure to assign the opener a name:

 

window.name = "main";
var map = window.open("newpage.html", "map");

 

If you include the following link in newpage.html, it loads our front page in the parent window:

 

<A HREF="http://www.docjs.com/" TARGET="main">Doc JavaScript</A>

 

The opener Property

The opener property sets or retrieves a reference to the window that created the current window. When a source document opens a destination window by calling the open() method, the opener property (of the destination window's window object) specifies the window of the source document. This property persists across document unload in the opened window, so it can be accessed even if the URL in the new window is changed.

The opener property is a reference of the parent window's window object. You can take advantage of this property to perform any action on the opener window via a script in the new window. For example, use the following script in the destination document to change the background color of the window that opened it:

 

window.opener.document.bgColor = "beige";

 

If you've got sharp eyes, you probably noticed the problem with this statement. We must check if the opener window still exists before attempting to access its properties. Here's the correct code:

 

if (window.opener && !window.opener.closed)
  window.opener.document.bgColor = "beige";

 

The opener property is very useful, because it completes the bidirectional connection between the opener window and the opened window. Take a look at the following form field:

<script></script>

Enter your favorite tech stock:

Let's see how this works. First, here's the HTML code for the form:

 

<FORM NAME="stockForm">Enter your favorite tech stock:
<INPUT TYPE="text" NAME="stockBox" SIZE="10" VALUE="">
<INPUT TYPE="button" VALUE="list" onClick="showList()">
</FORM>

 

Notice that the name of the form is stockForm, while the name of the text field is stockBox. The "list" button launches the showList() function. We have also implemented an onUnload event handler in this document's <BODY> tag, which invokes the remLink() function. Here's the code for these two functions:

 

<SCRIPT LANGUAGE="JavaScript">
<!--

function showList() {
  sList = window.open("stocklist.html", "list", "width=150,height=210");
}

function remLink() {
  if (window.sList && window.sList.open && !window.sList.closed)
    window.sList.opener = null;
}

// -->
</SCRIPT>

 

The remLink() function sets the subwindow's opener property to null when the current document unloads. The script in the new window checks the opener property before assigning a value to the text field. We must use this function because the new window's script shouldn't try to access the text box if a different document has been loaded in this window.

Here's the HTML code for stocklist.html:

 

<HTML>
<HEAD>
<TITLE>Stock List</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--

function pick(symbol) {
  if (window.opener && !window.opener.closed)
    window.opener.document.stockForm.stockBox.value = symbol;
  window.close();
}

// -->
</SCRIPT>
</HEAD>
<BODY>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="5">
<TR BGCOLOR="#cccccc"><TD><B>NYSE</B></TD><TD><B>NASDAQ</B></TD></TR>
<TR><TD><A HREF="javascript:pick('AOL')">AOL</A></TD>
<TD><A HREF="javascript:pick('CSCO')">CSCO</A></TD></TR>
<TR><TD><A HREF="javascript:pick('CPQ')">CPQ</A></TD>
<TD><A HREF="javascript:pick('INTC')">INTC</A></TD></TR>
<TR><TD><A HREF="javascript:pick('NOK')">NOK</A></TD>
<TD><A HREF="javascript:pick('SUNW')">SUNW</A></TD></TR>
<TR><TD><A HREF="javascript:pick('LU')">LU</A></TD>
<TD><A HREF="javascript:pick('AMZN')">AMZN</A></TD></TR>
<TR><TD><A HREF="javascript:pick('T')">T</A></TD>
<TD><A HREF="javascript:pick('MSFT')">MSFT</A></TD></TR>
</TABLE>
</BODY>
</HTML>
分享到:
评论
2 楼 jeho0815 2011-01-09  
jeho0815 写道

123
1 楼 jeho0815 2011-01-09  

相关推荐

    字符串 window.open() window.opener window.name window对象等的总结

    本篇文章将深入探讨`window.open()`、`window.opener`、`window.name`以及`window`对象的一些核心概念,同时通过两个带有注释的页面示例(001.html和main.html)帮助理解这些知识点。 ### `window.open()` `window...

    通过window.opener控制父窗体

    在发送窗口(子窗口)中,使用`window.opener.postMessage(data, targetOrigin)`发送消息,其中`data`是你要传递的数据,`targetOrigin`是接收窗口的源。在接收窗口(父窗口)中,需要添加事件监听器: ```...

    js弹窗并返回值(window.open方式)

    window.opener.result = result; window.close(); } // 用户在b.html操作后调用sendResultAndClose sendResultAndClose('用户的选择'); // 在原窗口监听新窗口关闭事件 window.addEventListener('message', ...

    解决window.opener=null;window.close(),只支持IE6不支持IE7,IE8的问题

    本文将深入探讨一个这样的问题:`window.opener = null; window.close()` 这段代码在IE6中能够正常工作,但在IE7及更高版本中却失效的问题。 `window.opener` 是JavaScript中的一个属性,它引用了创建当前窗口的...

    用window.open,opener实现网页间通信

    var txt = window.opener.document.getElementById("msg"); txt.value = "secondsaysyouwelcome"; }); } function secondcall(msg) { if (msg.indexOf("third") &gt; 0) { tw = window.open("third.html", ...

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

    window.opener.document.getElementById('orgNameId').value = name; window.close(); // 关闭子窗口 } } ``` 在上面的 `selectOrg` 函数中,当用户在子窗口中选择一个机构后,它会找到父窗口中的 `orgIdId` 和 ...

    刷新父窗口的多种方法

    window.opener.location.href = window.opener.location.href; ``` 这里的关键在于利用`location.href`属性来重新设置父窗口的URL,从而达到刷新的目的。需要注意的是,这种方式并不会触发浏览器的缓存机制,而是会...

    showModalDialog open弹出子窗口操作parent、opener父窗口及跨域处理

    opener.parentObj.elementObj.arrtr = 'str'; 3&gt; IE与FireFox对两个弹出窗口在跨域时的解析也有不同:通过window.dialogArguments操作父窗口时,在IE下不需要指定document.domain而在FireFox下则正好相反需要指定...

    JS window.opener返回父页面的应用

    window.opener.location.href = url; // 关闭当前的支付窗口 window.close(); } ``` 在实际应用中,需要注意跨域安全问题。由于同源策略的限制,`window.opener`在不同源之间可能无法正常工作。如果支付页面和...

    Javascript中封装window.open解决不兼容问题

    对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 var openWindow = function(url, options) { var str = ""; ...

    js关闭浏览器窗口及检查浏览器关闭事件

    [removed] function closeWin(){ window.opener=null; window.open(”,’_self’,”); window.close(); } [removed] &lt;a&gt;logout&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; 火狐默认不支持js关闭浏览器窗口,可以在about:...

    window.showModalDialog以及window.open用法简介

    window.opener.document.getElementById("name").value = "输入的数据"; ``` 这会将新窗口中的数据赋值给父窗口ID为"name"的文本框。 接下来,`window.showModalDialog`是Internet Explorer 4及以上版本引入的方法...

    window.location.href页面跳转的用法(区别于redirect)

    window.opener.document.location.reload(); ``` #### 总结 通过本文的介绍,我们可以看到`window.location.href`不仅能够实现简单的页面跳转,还能灵活地应用于复杂框架结构中的页面跳转及刷新。相比`Response....

    jsp 刷新父页面

    window.opener.location.href = window.opener.location.href 刷新以winodw.showModelDialog()方法打开的窗口 window.parent.dialogArguments.document.execCommand('Refresh'); 或 Response.Write("&lt;script&gt;...

Global site tag (gtag.js) - Google Analytics