Skip to content Skip to sidebar Skip to footer

Check If Changes Saved Before Unload

I have the following JavaScript EDIT: included assignments for changesSaved var changesSaved = true; $(document).ready(function () { $('.applyChanges').click(functio

Solution 1:

<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.";
    }
};

Solution 2:

window.onbeforeunload = function () {
     var changesSaved = confirm("You havent saved your changes. Are you sure you want to leave the page?");
    if (changesSaved) {
        returntrue;
    } else {
        returnfalse;
    }

Post a Comment for "Check If Changes Saved Before Unload"