...

View Full Version : Definitive answer to cross-browser window.onunload event



nuwanda
06-26-2006, 11:47 AM
Hi

Am I correct in saying that since Opera does not reliably implement the window.onunload event, that it is NOT possible to create a predictable cross-browser trap that executes a script on window close?

If there is a way to ensure trapping of the onunload event across all browsers, then please let me know.

Thanks.

Nuwanda

Kor
06-26-2006, 03:19 PM
try onbeforeunload. I don't know about Opera, but it works in IE and in the latest Mozilla's versions.

felgall
06-26-2006, 10:29 PM
Opera implements onunload. Don't think it implements non-standard events like onbeforeunload though.

nuwanda
06-27-2006, 04:47 AM
Opera does not implement onbeforeunload.

And, I've found it's trapping of onunload to be inconsistent.

In FF, IE and Opera this script causes an alert when the window LOADS.

NOTHING happens when the window is closed.

Reverse behavior, it seems.

=============================

<script type="text/javascript">

function cleanup(){
alert("Leaving page...");
}

window.onunload=cleanup();

</script>

=============================

But this (note the ommision of brackets after cleanup), causes an alert only on window close (as it should) in FF and IE but NOT in Opera.

=============================

<script type="text/javascript">

function cleanup(){
alert("Leaving page...");
}

window.onunload=cleanup;

</script>

=============================

Hence my problem!!

brothercake
06-27-2006, 11:10 AM
Am I correct in saying that since Opera does not reliably implement the window.onunload event, that it is NOT possible to create a predictable cross-browser trap that executes a script on window close?
Yes, you are correct in saying that (though not just for that reason)

But if you opened a window yourself you're okay, because you can refer to its boolean "closed" property.

nuwanda
06-28-2006, 02:36 AM
Yes, I can refer to the opened window's closed property, but only from the opener. But, all my code executes in the new window and not the opener.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum