View Full Version : Javascript: Close after window loses focus
camargo
June 3rd, 2005, 07:55 PM
Hey all,
I need to have a popup window close after it loses focus. How can I accomplish that? Here's the example:
http://www.wirelessinteractive.com/24_ant_indoor.php
Click on the "Consider the Total Solution" button and observe the popup. I need that popup to close itself if someone follows the link inside of it. Any ideas?
Thanks in advance!
Kirupa forums ROCK!
Ankou
June 3rd, 2005, 08:11 PM
Use onclick and window.close() to close the window that was popped open -- like so:
<a href="http://www.google.com (http://www.google.com/)" title="Google" target="_blank" onclick="javascript: window.close();">Google</a>
Obviously change the link to whatever page you want to open.
lostinbeta
June 3rd, 2005, 09:45 PM
or you can call a window.close() in the documents onBlur.
<body onBlur='javascript:window.close();'>
Note: This will bring up a 'Are you sure you want to close this window' alert if the window is the main window and not a window opened up by a link (such as a pop-up window). And no, there is not a way to get around that.
nevermind, just went to your example site... probably not the best method in this case
camargo
June 7th, 2005, 04:09 PM
Well thanks to you both. A huge help here. It only seemed obvious once I got the answer. :block:
Haha. Thanks guys.
unchew
June 7th, 2005, 04:38 PM
or you can call a window.close() in the documents onBlur.
<body onBlur='javascript:window.close();'>
Note: This will bring up a 'Are you sure you want to close this window' alert if the window is the main window and not a window opened up by a link (such as a pop-up window). And no, there is not a way to get around that.
nevermind, just went to your example site... probably not the best method in this case
Yes there is away to close the window without a prompt!
lostinbeta
June 7th, 2005, 05:04 PM
No there isn't, not in recent browsers.
There used to be the ability to set the window.opener to null to cancel it out. But they fixed that 'hack' in the last release of IE and that method never worked in gecko based browsers (Netscape, Mozilla, FireFox).
And in tabbed browsers (such as Mozilla or FireFox), window.close() doesn't work at all unless it is a pop-up for the risk of closing tabs that weren't meant to be closed.
If you know of a newer or alternative way, please do share because I have not for the life of me been able to figure that out.
Though, one should NEVER be closing the main window with a link, the user should do that themselves the normal way. It's a usability no-no, which is why you get the prompt in the first place.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.