JavaScript + onbeforeunload

window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

See the MSDN article on onbeforeunload

Also there is a similar question in SO


Try:

window.onbeforeunload = function() { ...your code... }

From my experience onunload works differently in different browsers. Why dont you use another event handler called onbeforeunload instead. It should work. Onbeforeunload will execute first before the window closes, so that should solve your problem.