Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I confirm a browser window is open, with Javascript?

Tags:

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.

like image 345
Alister Bulman Avatar asked Oct 21 '08 10:10

Alister Bulman


People also ask

How do you check if a window is already open in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

How do I open a browser using JavaScript?

open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values. Approach: To open a new tab, we have to use _blank in the second parameter of the window. open() method.

Which window method is used to open the specified URL in JavaScript?

open() function contains the URL string of a webpage, which you want to open. If you do not specify any URL in this function, it will open a new blank window (about:blank). Passed URL will load into a new tab/window. URL will load into the parent window or frame that is already opened.


1 Answers

This excellent, comprehensive article ("Almost complete 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.

like image 106
splattne Avatar answered Sep 27 '22 23:09

splattne