onbeforeunload Event Browser Support
Today, I was reading about the onbeforeunload event. I have a custom event system for Herd Hound app, so I wanted to destroy all event handlers before allowing the browser to navigate away from the page. For this I thought I’d use the onbeforeunload event, but then I read it wasn’t widely supported. As most frontend engineers, I have a multitude of browsers installed on my laptop, so I did a bit of testing.
So here’s the code to test the event:
window.onbeforeunload = function() {
alert('bye');
};
You make a simple page, put this snippet between SCRIPT tags… you know the drill, right? Then you just open the page, wait for it to load, and close it. It should say ‘bye’ when you close it. Thus far, this works in the following browsers:
- Google Chrome 11
- Firefox 3.6
- Firefox 4.0
- Internet Explorer 9 (should work in 8 as well)
- Safari 5.0
It does not work in Opera 11. For my own purposes, it is enough that it works in IE to prevent possible memory leaks created by unbound event handlers, although I suspect this precaution is an overkill since these are not DOM events. At any rate, I at least know what browsers support this event now. :)




