I'm opening a new browser window from my site for some of the members. However, some may later close it, or it might have initially failed to open.
Is there a snippet of fairly plain Javascript that can be run on each page to confirm if another browser window is open, and if not, to provide a link to re-open it?
[clarification:] The code to check is a window is open would be run on other pages - not just in the same window and URL that opened it. Imagine a user logging in, the window (tries to) open, and then they surf around in the same tab/window (or others) for some time before they close the 2nd window (or it never opened) - I want to be able to notice the window has been closed some time after the initial attempt at opening/after it's closed, so I'm not sure that checking the javascript's return from window.open() (with popup_window_handle.closed) is easily used, or indeed possible.
I'm opening a new browser window from my site for some of the members. However, some may later close it, or it might have initially failed to open.
Is there a snippet of fairly plain Javascript that can be run on each page to confirm if another browser window is open, and if not, to provide a link to re-open it?
[clarification:] The code to check is a window is open would be run on other pages - not just in the same window and URL that opened it. Imagine a user logging in, the window (tries to) open, and then they surf around in the same tab/window (or others) for some time before they close the 2nd window (or it never opened) - I want to be able to notice the window has been closed some time after the initial attempt at opening/after it's closed, so I'm not sure that checking the javascript's return from window.open() (with popup_window_handle.closed) is easily used, or indeed possible.
Share Improve this question edited Oct 21, 2008 at 12:20 Alister Bulman asked Oct 21, 2008 at 10:45 Alister BulmanAlister Bulman 35.2k9 gold badges74 silver badges112 bronze badges3 Answers
Reset to default 4This excellent, prehensive article ("Almost plete control of pop-up windows") should answer all your questions about javascript popup windows.
"JavaScript 1.1 also introduced the window closed property. Using this property, it is possible to detect if a window has been opened and subsequently closed. We can use this to load a page directly into the opener window if it still open for JavaScript 1.1 enabled browsers:"
<script language="JavaScript"><!--
function open_main(page) {
window_handle = window.open(page,'main');
return false;
}
//--></script>
<script language="JavaScript1.1"><!--
function open_main(page) {
if (opener && !opener.closed) {
opener.location.href = page;
}
else {
window_handle = window.open(page,'main');
}
return false;
}
//--></script>
<a href="example.htm" onClick="return open_main('example.htm')">example.htm</a>
Addition: You can get the window handle back in another page by referring to the popup's name this way:
window_handle = window.open(page,'myPopupName');
I guess in your case you should think about a consistent way to create all the names of the popup windows throughout the entire application.
var myWin = window.open(...);
if (myWin.closed)
{
myWin = window.open(...);
}
There is no universal way to check it as each pop-up blocker has a different behaviour. For example, Opera will not shown the pop-up but the widow.closed will return false as behind the scene Opera keep a shadow copy of the blocked window, just in case the user would like to see it anyway.