Show browser’s default alert when you leave page without saving the data

var isTouched = false;

// make isTouched variable true by any activity, like any input text changed

window.addEventListener("beforeunload", function (e) {
    if (isTouched) {
        var confirmationMessage = 'Changes you made may not be saved.';

        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    }

});

 

+1
1
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0