Warn user about unsaved data before leaving page


When user changes some fields or data on page and without saving just click on link somewhere else and move away from page, In such case sometimes it need to confirms from user about changes he has done will be lost, and if he sure about this. This can be achieved with the just below small block of code.
This will be good if you use this script in your master page so it will get apply to all pages.


 

$(document).ready(function () {

var warnOnUnload = "";

$('input:text,textarea, select').on('change', function () {
warnOnUnload = "Leaving this page will cause any unsaved data to be lost.";
$("form").submit(function () {
warnOnUnload = "";
});
window.onbeforeunload = function () {

if (warnOnUnload != '') {


return warnOnUnload;

}
}
});
});


 

In application this will look like as,

Comments