View Full Version : onUnload not working properly...?

08-23-2004, 11:07 AM

I have a popup window which when closed, I want to call another function to then open another window.... I've used the following code in the first popup window...

<script language="JavaScript1.2">
function closeWindow() {
popup = window.open("leave.php","leave","height=50,width=50,scrollbars=no");

and the above function is called in the main body tag of the first popup..

<body onUnload="closeWindow()">

The reason for this is that when the user closes the first popup, I need to call a PHP script to end their session, I think this is the best way to do it... by opening a second window, calling the script and self.closing the window immediately... the problem is, the second window just isnt opening.. the first one closes without launching the second one...

any help is much appreciated!

thanks!! :thumbsup:

08-23-2004, 12:01 PM
the problem is, the second window just isnt opening.. the first one closes without launching the second one...

It always helps to indicate which browser in which your code doesn't work.

It works fine for me in IE6.0 and Firefox 0.9.3.

You can also write it:

window.onunload = closeWindow;

If you are using Firefox, are you sure that it isn't being blocked by the built-in popup blocker....?


08-23-2004, 12:08 PM

i think you're fighting a losing battle with this... you can sorta do it for IE, but you can't for Moz.

in IE, you've got the onbeforeunload event which get's called just before the browser window closes. with this you can call any extra functionailty you need for browser-session management. please note, however, as of XP-SP2, this may now be no longer working for the majority of your IE users, since SP2 blocks auto popups. i think it may do this without any ability to over-ride the behaviour where FF does for instance.

this may, of course, if your site is actually an application for a closed audience system, such as an intranet, but it would be for a secure website, such as webmail account. in the case of the former, the weblication could be added to the trusted zone, thereby getting round any security restrictions. but you wouldn't be able to count on that for a website.

you may have to figure out a way round the session issue, without having to use popups. as awkward as it is.

Willy Duitt
08-23-2004, 12:09 PM
The problem is that your window is closed before the onunload event can be fired....

08-23-2004, 12:21 PM
thanks for the replies guys...

It's a fairly closed audience, but still using the internet... not an intranet. What I am doing is for a client where they can edit a webpage in a WYSIWYG Editor, which opens in a new window.. a file is written to the server temp folder stating that this file is in use. If another user tries to edit the same file, it checks the "in use" files and will stop them editing it.

The reason for the need to call a PHP script when the user closes the window is that I need to remove the filename (ie: call the PHP script) to "un lock" the file. It is actually calling the script, but the popup is not appearing...

if anyone has any better ideas, I'd be v interested to hear them! Is there a way to remove the "X" close button of a window?? (probably unlikely?!)

Hmm..just had an idea.... I could put onunload="return closeWindow()" then have the closeWindow function saying somethng like

alert("please close this window using the button inside the page");
return false;

that might work?? Then I can have a button inside the popup which simply calls another PHP page... much easier!! :)

08-23-2004, 12:25 PM
just tried the above... and It does popup the alert, but then I get a message:

"Are you sure you want to navigate away from this page?


Press OK to continue, or Cancel to stay on the current page"

ps.. had to use "onbeforeunload=..." for it to work also....

any way of getting rid of this message?? or forcing it to think you've pressed "cancel" ?

08-23-2004, 12:38 PM
ive also heard about "frameless" containers?? or something like that... ie: a popup with out any toolbars or buttons etc etc...... that might be an option?

08-24-2004, 07:43 AM
Try this:

function closeWindow() {
var popup = new Image();
popup.src = "leave.php?"+Date.parse(new Date()); //prevent caching