Search code examples
javascriptonbeforeunloadonunload

"onbeforeunload" not firing on unload


I stuck this in a .js file...

window.onbeforeunload = alert('onbeforeunload');

But it fires when the page is loaded, not when it is unloaded.
Does anyone know why?


Solution

  • You have to wrap it... try this instead:

    window.onbeforeunload = function(){alert('onbeforeunload')};