How to tell users unsaved changes if they try to leave a web page

Possible duplicate:
How to stop page unloading (moving) in JS?
Request for unsaved changes when exiting a web page

I have a php form that I created in Dreamweaver containing only text fields that retrieve one record at a time from my database. On this page there is also a button “Update record”, which, when clicked, updates the database record with any changes made to the data in the form. I want that if the user somehow edits the form data and tries to leave the page without clicking the "Refresh record" button, it will be suggested that the page contains unsaved changes, and they will be asked to check whether they want to leave the page or not ( possibly using some javasript if possible).

Any help is greatly appreciated.

+1
source share
2

onbeforeunload , : , , , .

, . , , , .

window.onbeforeunload = function(e) {
    if (unsavedChanges) return 'Dialog text here.';
};

, . alert confirm onbeforeunload, onunload .

+7

"on" api jquery.

 var flag = true; // set this var according to your use.
 $(window).on('beforeunload', function(){
     if(flag) {
        return "It looks like you have input you haven't submitted."
      }
 });
+1

Source: https://habr.com/ru/post/1615223/


All Articles