Reputation: 43
The following code produces a javascript popup when a page is loaded and then closes the popup window when the parent window is closed.
<script language="JavaScript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
player.close();
}
window.onload = load;
window.onunload = unload;
</script>
This is working fine, however it closes the popup window when switching pages on the site. Is there any way this can be altered to keep the popup window until the actual URL/Site window is closed?
Upvotes: 0
Views: 296
Reputation: 6516
This may help you:
<script language="Javascript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
if (!(window.location.equals('http://yourSiteName.com'))) {
player.close();
}
}
window.onload = load();
window.onunload = unload();
</script>
Upvotes: 0
Reputation: 46057
I think you have to check the status of the parent window from the child, instead of vice-versa.
This code has not been tested, but I'm taking a stab at the concept:
window.opener.onunload =
setTimeout(
function(){
if (!window.opener) self.close();
}, 1000);
Upvotes: 1
Reputation: 71918
Short answer is NO, the only way to hook into the event that closes the window is onunload
, which fires when the document is unloaded (like when you navigate to another page).
Maybe you could check window.opener
on the popup (with a timer, or onfocus), to check if the window that originated the popup still exists, then close the popup if it doesn't.
Upvotes: 0