Check if changes saved before unload

后端 未结 2 1340
离开以前
离开以前 2021-01-22 10:41

I have the following JavaScript

EDIT: included assignments for changesSaved

 var changesSaved = true;

    $(document).ready(function ()         


        
相关标签:
2条回答
  • 2021-01-22 10:58
    <body onbeforeunload="return bye()">
    
    function bye(){
        if(changesSaved) {
            return "You havent saved your changes."
        }
    }
    

    This is how I do it.

    or in pure JavaScript:

    window.onbeforeunload = function() {
        if (changesSaved) {
            return "You haven't saved your changes.";
        }
    };
    
    0 讨论(0)
  • 2021-01-22 11:10
    window.onbeforeunload = function () {
         var changesSaved = confirm("You havent saved your changes. Are you sure you want to leave the page?");
        if (changesSaved) {
            return true;
        } else {
            return false;
        }
    
    0 讨论(0)
提交回复
热议问题