Benjamin Yogman

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromBenjamin Yogman
ToMe
SubjectDetecting whether a browser window was opened with ctrl-n
Date8 April 2004 16:17
I can detect window closes in ie with javaScript, and was hoping to plug
that back into some session management logic to reduce the need for slow
timeouts.  However, I don't know how to detect, or whether I can detect
whether a browser window was opened with file->new window / ctrl-n.  When
they do so, in our application, it reuses the session, so I don't want the
same window closing logic to fire.  

Actually, what would be cleaner, would be if there's a way to detect back
at the server that a request came from a certain browser window, so you
could close the original window, and keep the session valid on the window
that you opened, only cleaning up the session when both windows are closed
(keep a window count in the session).  I have no idea whether that's really
possible, I'm pretty new into this stuff.  Any perspective you can provide
is appreciated.

Because this is performance related, it's not crucial, but it would be nice
if I could do all this with Mozilla as well.
FromMe
ToBenjamin Yogman
SubjectRe: Detecting whether a browser window was opened with ctrl-n
Date8 April 2004 18:06
Benjamin,

you came to the right place ;)

There are several things that would help you here, and you will be happy to
hear that these techniques are so well supported, they will work in all
current browsers: IE, Mozilla, Opera, Safari, iCab, NS4 ... I could go on.

1) you can tell if the current window was opened with script (or even
target="_blank" in IE), because the JavaScript window.opener property will
be a pointer back to the opening window: if( window.opener ) { etc. }
You cannot close the opening window unless it was originally opened with
script: window.opener.close()
You can also check window.opener.closed to see if the opener has been
closed

There is no server side equivalent to this property.

Be warned that using file->new reuses the session, but clicking on the IE
icon again will not.

2) you can also tell if a window was opened by a link or script in another
window by checking document.referrer (note the spelling), which should
contain the url that opened the page, but is not a reference to the opening
window.

Most server side scripts also offer the referer information somewhere (in
PHP it is $_SERVER['HTTP_REFERER'] - again, note the different spelling)

Note that the Opera browser (my personal favourite) allows users to disable
the referrer functionality, although most users will not disable it.


Hope you manage to decypher enough of that to use it in your application.
If you have any problems, feel free to get in touch again.

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromBenjamin Yogman
ToMe
Subjectonbeforeunload, and self.close () -- they don't work in Mozilla?
Date16 April 2004 15:44
I am confident that my session management trickery will work... at least
for IE.  My problem now is mozilla (I don't know about Safari yet, testing
that tommorow).  Essentially, what I need to do is detect the closing of a
window, and then load another page that will close itself automatically.

Currently, the code I'm using (which works for ie) looks like this:

For the window count incrementing page that detects the window close and
fires up the window count decrementer:

<head>
<script type="text/javascript">
<!--
function decrementWindowCount () {
    window.open("releaseWindowFromCount.html");
    // this will be replaced with a jsp doing just that, and invalidating
    // the session if you get down to zero
}
//-->
</script>
</head>

...

<body onbeforeunload="decrementWindowCount()">
</body>


For the window count decrementing page that closes itself automatically:

<body>
<script type="text/javascript">
<!--
//alert("closing!");
self.close();
//-->
</script>
</body>

I must confess that I was a little surprised that the latter didn't work in
mozilla, any thoughts?  Also, is there an event other than onbeforeunload
that I can trap in mozilla when the user closes the browser's window?

As a final note, and this is sort of pie in the sky, it would be nicer if I
could avoid flashing an empty window at users when they close up.  Is there
a way to make the first page stay unclosed while I load the window count
decrementer so I can do it invisibly?

Anything you can tell me is appreciated.

Cheers,
Ben
FromMe
ToBenjamin Yogman
SubjectRe: onbeforeunload, and self.close () -- they don't work in Mozilla?
Date16 April 2004 20:18
Ben

onbeforeunload is a non-standard microsoft event. None of the other
browsers support it. It is supposed to work like this:
onbeforeunload="event.returnValue='I want you to stay';"
And it will prompt the user not to close the page.

The standard one is onunload. unlike onbeforeunload, it cannot cancel
closing the window, but all browsers support it.
onunload="decrementWindowCount()"
The event will trigger when the user leaves the page or closes the window.

When you attempt to open a new window, that is considered to be a popup, so
the popup blockers in Opera, Mozilla and Safari will block it if the user
has enabled them, so you are correct to want to avoid using a popup. I have
a couple of techniques for loading pages from the server without using a
popup.
One is to load an image:
( new Image() ).src = 'releaseWindowFromCount.jsp'; //serve an image
The other is to load external data
http://www.howtocreate.co.uk/loadingExternalData.html
Both suffer from a problem, they may not have the chance to do anything
before the window closes. For this reason, the popup is the safest
technique, because you can at least detect if they block it, and ask them
not to block it next time:
var x = window.open('blah');
if( !x ) { alert('Please do not block popups on this site'); }

self.close() should work in all browsers, provided that YOU opened the
window using script.
If it is causing you problems, use window.close() instead (these should be
equivalent but you never know ... ).

Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.