tags:

views:

47

answers:

3

What is the best way to detect if a user leaves a web page without saving?

i have searched before i post this question what i haven't found related topic to deal with asp.net (i have controls dropdwonlist, listbox, textbox....)

A: 

I would use the JQuery serialize method to save off the form's state in the page's OnLoad event. Then serialize the page again in the OnBeforeUnload event. If the values are different, then the page has changed. You'll have to add a flag to know whether or not the page is unloading because the "Save" button is being clicked. If the values are different and the user didn't click "Save", then display the "Do you want to save before leaving?" box.

David
would you mind showing some sample code?
Abu Hamzah
A: 

One very simple way is to do it the StackOverflow way:

http://www.jonstjohn.com/node/23

Basically it boils down to setting a method to be called in the onbeforeunload javascript event.

Chris Lively
+1  A: 

There is a similar question to what your asking that has an accepted solution. http://stackoverflow.com/questions/147636/best-way-to-detect-when-user-leaves-a-web-page

jostster