Wednesday, June 25, 2008

Does FireFox support beforeunload event?

The answer is yes. FireFox does support beforeunload event. However, you can't expect it to work if you put its event handler into the <body> tag as we do in IE.
In IE, there are several ways to define the onbeforeunload event handler


(1)<body onbeforeunload='confirmExit();'>

(2)document.body.onbeforeunload=confirmExit;

(3)window.onbeforeunload=confirmExit;

function confirmExit(){
return "You are about to leave this page. Are you sure?";
}

in FireFox, you can only use the (3) to trigger the unbeforeunload event handler. It frustrated me first, because I thought they are all same, but why I didn't get what I expected. I guess it is not supported very well and the first two approaches don't work in FireFox. So if you want to use onbeforeunload, please use the (3) to make it work on both IE and FireFox.

2 comments:

JO~ said...

thanks 3 option works for me...

Anonymous said...

I every time emailed this blog post page to all my associates, since if like to read it after that my friends will too.


my webpage; illuminatural 6i reviews