Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript + onbeforeunload

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.

like image 848
pradeep Avatar asked Aug 13 '09 06:08

pradeep


People also ask

What triggers Beforeunload?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.

What is the difference between Onbeforeunload and Onunload?

onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.

What is Onunload in JavaScript?

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.). Note: The onunload event is also triggered when a user reloads the page (and the onload event).


1 Answers

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

See the MSDN article on onbeforeunload

Also there is a similar question in SO

like image 113
rahul Avatar answered Sep 19 '22 14:09

rahul