PDA

View Full Version : window.opener help


Jimbo
01-27-2003, 11:51 PM
What is the "window.opener" property used for? I have seen it used many times and can't seem to figure out its use.

whammy
01-28-2003, 01:18 AM
It's used to communicate with the "parent" window that opened the "child" window in question.

For instance you could update a variable in the "opener" (parent) window from a "child" window using that.

Probably not the best or most technical explanation... ;)

zoobie
01-28-2003, 07:55 AM
It simply tells a new window to open. http://www.cg9com.web1000.com/smilies/lol.gif

jwite2003
04-05-2003, 10:35 PM
Whammy: could you show an example of using window.opener

I'm trying to use it right now to set a variable in the parent window... I've looked at a number of web tutorials that use it as follows:

window.opener.<formname>.<server control ID>.value = <any value>

However when I try to replicate this, I don't get any choices after:
window.opener

thanks,

cheesebagpipe
04-05-2003, 11:01 PM
For every browser instance (window) a user has open, JS manages a window object to provide 'scriptability' for that window. It's easy to make your own references to a pop-up window you opened yourself, since the window.open() function returns the newly minted window object:

var my_new_win = null;
function open_my_new_win() {
my_new_win = window.open(.......

You can now script my_new_win...my_new_win.close(), e.g. But suppose you wanted to go back in the other direction: script the original (opener - 'parent' is misleading, since it applies to frames)...well all window objects have an opener property, normally set to null, but set with a reference (pointer) to the opening window (object) when there is one. btw, not necessary to refer to it as 'window.opener', though most people seem to - it's always defined so opener is sufficient.

if (opener && !opener.closed) opener.document.form_name.element_name.value = whatever

For this kind of hierarchial (DOM 0) reference, always use names (not ids), and include the document in the path. Note the object checking (the .closed property is set to true when the window is closed & therefore unscriptable).

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

whammy
04-05-2003, 11:07 PM
And, here's an example like you asked for:

parent.htm:

<html>
<head>
<title>Parent</title>
<script type="text/javascript">
<!--
function openWindow(URL) {
newWin = window.open(URL,'childWin','width=300,height=100');
newWin.focus();
}
// -->
</script>
</head>
<body>
<a href="child.htm" onclick="openWindow('child.htm'); return false" />Link</a>
<form id="form1" action="javascript://">
<input type="text" name="test" value="" />
</form>
</body>
</html>


child.htm:

<html>
<head>
<title>Child.htm</title>
<script type="text/javascript">
<!--
function doSomething() {
if(opener && !opener.closed) {
opener.document.forms[0].test.value = "Yay!";
self.close();
}
}
// -->
</script>
</head>
<body>
<a href="child.htm" onclick="doSomething()" />Yay!</a>
</body>
</html>

jwite2003
04-06-2003, 05:44 AM
Thanks CheeseBagPipe and Whammy :cool: