Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript to close another window (MyWin2) IF it exists, HELP???

Status
Not open for further replies.

mb22

Programmer
Sep 4, 2002
258
US
how do I write JavaScript to close another window say "MyWin2" IF it exists.

I want to check if a particular window exists, then close it first...... because I re-open that particular window again in several different ways (i mean like with different controls visible) depending on the options selected.

I don't want several copies of the same window around if the user forgets to close it after using it!

thanks!
 
You could use some like:
Code:
<html>
<head>
	<title>User Title...</title>
<script language=&quot;JavaScript&quot;>
var theWin;
function popUp() {
	theWin = window.open(&quot;[URL unfurl="true"]http://www.google.com&quot;,&quot;TheWindow&quot;,&quot;width=300,height=300&quot;);[/URL]
	theWin.focus();
}
function popUp2() {
	if (theWin != null) {
		theWin.location.href=&quot;[URL unfurl="true"]http://www.altavista.com&quot;;[/URL]
		theWin.focus();
	} else {
		alert(&quot;The window was closed... so open a new one!&quot;);
		theWin = window.open(&quot;[URL unfurl="true"]http://www.altavista.com&quot;,&quot;TheWindow&quot;,&quot;width=300,height=300&quot;);[/URL]
	}
}
function closeIt() {
	if (theWin != null) {
		theWin.close();
	}
}
</script>
</head>
<body>
<a href=&quot;javascript:popUp()&quot;>Google</a><br>

Change the window location to <a href=&quot;javascript:popUp2()&quot;>AltaVista</a><br>
<a href=&quot;javascript:closeIt()&quot;>Close Window</a>
</body>
</html>

The problem occurs, when you close the window using the &quot;x&quot; icon in the top right corner... however, using the &quot;onunload&quot; event within the window, you could modify the &quot;opener's&quot; variable and set it to null (window.opener.theWin = null).

Does this help?

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 

Something like this should do the trick


function closeSpecific(x){
if(x)
x.close()
}

closeSpecific('MyWin2')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top