Click to See Complete Forum and Search --> : perform a javascript when a window is closed


Jessica Nieves
October 16th, 2002, 03:17 PM
Hi everyone!
I need to know how to perform some JavaScript code when a window is closed. Does anyone know how do I do that??

I tried using unUnload on the bosy, but this execute the javascript everytime the page is unloaded (link is clicked, back button is pressed, fwd button is clicked... etc) and it doesnt helped me out. Is there anything as onClose??

Please I need some help!!
:rolleyes:
Thanks
Jesy

Waldo2k2
October 16th, 2002, 07:26 PM
well, window.closed is a boolean value that returns true or false. I've tried to tinker with it, but i think if you close the window that the code was issued from, it halts all scripting processes. So, i don't think you can use it in that way. But, you can use it on a child window.

websmith99
October 17th, 2002, 06:31 PM
For the cases where the onUnload is invoked by clicking on a link, you can simply set a flag for each link and then check the value of the flag on the onUnload event.

The cases of forward and back is another story. Will this be in a popup window or the main browser window?

Jessica Nieves
October 18th, 2002, 07:45 AM
Thanks both for your replies...
I tried using the window.closed but you were right waldo, it didnt work with a parent window.
Anwering Websmith's question... my window is a parent window.
About setting a flag in each link, it is difficult because my site is a big one ans it has too many links.

My goal with all this is to sign off the user when he close the window... any suggestions are welcome.

Thanks
Jesy

Waldo2k2
October 18th, 2002, 03:48 PM
well, you could you could launch your main site from another window that will blur when the child opens, then you could easily use window.closed()....i wonder if it works for frames as well....

ShawnDev
October 18th, 2002, 08:35 PM
According to the official(?) DOM reference at Mozilla (http://www.mozilla.org/docs/dom/domref/), there is an onClose (http://www.mozilla.org/docs/dom/domref/dom_window_ref56.html#1018253) event for the window object in Netscape 6+, Mozilla and any other browser based on the Gecko engine. But my brief testing couldn't get it to fire.

I also thought maybe you could do something with looking at the most recent window.history entry and comparing that to the current location... or something along those lines. But I couldn't get the history object to work at all in either Mozilla 1.1 or IE 5.5, so I don't have any specific suggestions in that regard.