View Full Version : bringing open window to top

11-05-2012, 02:59 AM
I have some code that uses window.open to open a window in response
to a mouse click event handler.

If the user has used the button to open the window,
then brings the window.opener window to the front,
then clicks the button again,
the window that is opened by code stays in the background.

How do I get it to come to the front when the user clicks the button?


Old Pedant
11-05-2012, 06:10 AM
<script type="text/javascript">
var theWindow = null; // make this a global variable

... somewhere else in the code ...
theWindow = window.open("...the url...", "theWindowName" );

.... anywhere else in the code ...
if ( theWindow != null ) { theWindow.focus(); // bring it to front }


11-05-2012, 09:12 PM
If I the user uses the browser window close button (or short cut)
Javascript will not process an onclose event in that case. (using Firefox to develop,
I have not tried this in other browsers, but I am guessing the same for them)
So, the global variable representing the window does not reset to null.
Then opening the window with the button will open a nonexistent window.

That provokes another two part question:
In spite of the paranoia about monitoring, it would be reasonable for a
developer to determine if the user has used the window's close button
or short cut to close a window, AND, it would be an enormous convenience
to the whole net if the browser sent a query to the server when the last
window displaying specific content was closed.
(I also have done a lot of php and from the server side it would vastly
simplify things like session handling and cleanup, even for non secure
type tracking)

11-05-2012, 09:51 PM
Closing a web page (such as by closing the browser window or simpy swapping to a different page in that window will trigger an "unload" event. There is no such thing as a close event.

Old Pedant
11-05-2012, 10:20 PM
It's not a full solution for your question/problem, but you could do this:

<script type="text/javascript">
var theWindow = null; // make this a global variable

... somewhere else in the code ...
theWindow = window.open("...the url...", "ZAMBONI" );
theWindow.onunload = function( ) { theWindow = null; } // see below

.... anywhere else in the code ...
if ( theWindow != null && theWindow.name == "ZAMBONI" ) { theWindow.focus(); // bring it to front }


If you only want to set theWindow to null if the user navigates to another domain or completely closes the window, you could pull a few hacky tricks.

For example, when the window unloads, just do a setTimeout() and, after a second or two, have your code go see if it can access the window contents. If it can, then the window is still in your domain. If not, it's either closed or in another domain.

Just throwing ideas in the wind here. Play around. You can likely find a way to do most if not all of what you are trying to do.

11-06-2012, 01:03 AM
I know there is no close event.
What leads me to believe the using the browsers chrome close button,
or window closing short cut will not be processed by javascript is that I
have been experimenting with code that runs when the unload event handler
is supposed to be called. For instance having an alert called when the window
closes won't happen, or at least I could not get it to.
I have an app the opens a window and allows user to edit string content.
I have had to write a specific warning not to use the chrome window closing
button or window closing short cut because it will dump all the changes that
have been made. I want the user to be shown a warning confirmation dialog
to allow them to save to the opening window or the server. This is what happens
in applications that run locally, but not browser based applications running javascript
or ActionScript because they are not allowed local file system access.
What would happen if I closed and used history to navigate back to the browser window
when I am composing this reply?

Old Pedant
11-06-2012, 01:45 AM
Have you looked into window.onbeforeunload?

What it can do is limited, but it's still pretty useful for stuff like this.

11-06-2012, 04:39 AM
This is an IE specific event isn't it?

anyhow here is what appears to work for open and closed windows,
assuming the window.opener window is still opened:

this.helpWindow = null;
this.show_help = function()
if(this.helpWindow == null || !this.helpWindow.name )
this.helpWindow = open('SC_docs/calc_manual.html', 'Calculator_2_manual', 'height=600,width=700,scrollbars=yes,resizeable=yes');

<window name>.document.close() is for the sake of Firefox which will
never finish loading the window without it (or some versions of Firefox.)