JavaScript + onbeforeunload

pradeep picture pradeep · Aug 13, 2009 · Viewed 97.4k times · Source

I have a query regarding my application. Whenever user closes browser window accidentally I'd want to do some clean up operations before that. I have used onunload event but problem is this event is sometimes is firing and sometimes doesn't. What should I do, is there better way to handle such type of problem.

Answer

rahul picture rahul · Aug 13, 2009
window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

See the MSDN article on onbeforeunload

Also there is a similar question in SO