简体   繁体   中英

Saving Data Before Leaving Page

I have an Asp.net MVC 5 application which got some pages need to be saved data if the users accidently close the page or enter another url...

So I got stuck because I tried to use onbeforeunload javascript event to confirm whether the users want to leave page or not. But if they hit OK I can't find any event to handle my saving data function.

My question: Is there any way to confirm users if they are leaving then save data if they hit OK?

Thanks

As state by @charlietfl the usage of onbeforeunload is really just confirming whether the user want to leave the page or not. If they chose to leave, then that is it, no function can be run.

One way of achieving something you want is periodicaly save users' changes (or provide a save button of some sort), a variable will be holding whether a save action has to be taken or not.

When user about to leave the page, check that variable, if all change has been saved, then nothing showed. Else prompt user to save change before leaving to prevent lose work.

for a example look at this SO question: check if changes saved before unload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM