FIXED Re: [thelist] javascript: closing a window opened from a previous page

Scott Dexter dexilalolai at yahoo.com
Fri Apr 30 09:43:40 CDT 2004


> 
> How about the opened window closing itself, when its 
> window.opener.location== B?
> 

Good idea. This is what I ended up coming up with, and it will work
for my use. An interesting thing I found out here was the opener.name
may or may NOT change depending on how the form is submitted.
Submitting the form through the javascript call below
(theform.submit()) works regardless of the form method (POST or GET).
IF you submit with the <form> tag, using GET doesn't change the
opener.name attribute; in fact, it gets the name of the page being
submitted TO. Interesting. But I gots it working, and code is below
for your entertainment. I feel meek that it took me this long, but
anyhoo....

--pageA.htm--
<script language="javascript">
window.name = 'pageA.htm';

function doSubmit(){
// open a child window, and it will close itself
var newWin =
window.open('child.htm','child','scrollbars=no,resizable=yes,dependent=no,height=500,width=620');
document.testForm.submit();
}

</script>
--pageA.htm--

--pageB.htm--
<script language="javascript">
window.name = 'pageB.asp';

</script>
--pageB.htm--


--child.htm--
<script language="JavaScript">
var origOpener = window.opener;
var origName = origOpener.name;

function OpenerWatch(){
// watch for changes in the opener.name
setTimeout('testOpener(origName)',1000);
} //end OpenerWatch

function testOpener(strName){
// if the opener reference is lost, close this window
if (opener && opener.name!=strName) {
	alert('Closing -- the opener page changed: \n original name: ' +
strName + '\n name now: ' + opener.name);
	self.close();
} else {
	alert('Opener still there -- ' + opener.name);
	OpenerWatch();
}
}// end testOpener
</script>
...
<body onLoad="OpenerWatch()">
...

--child.htm--


Thanks for listening,
Dex


More information about the thelist mailing list