Difference between onbeforeunload and onunload

onunload is responsible for executing an instruction when the page is closed. It also causes issue with IE and AJAX.

onbeforeunload is more efficient because it does not run in competition with the actual closing of the window and is triggered before onunload

I know Opera used to not acknowledge onbeforeunload – not sure if they’ve fixed that, but I always register the listener for both to be safe:

window.onunload = window.onbeforeunload = (function(){...

Leave a Comment