This is what I use, put all this code in a separate JS file and upload it to your header file so you don't have to copy this over and over again:
var unsaved = false; $(":input").change(function(){
EDIT for $ not found:
This error can only be caused by one of three reasons:
- Your JavaScript file does not load correctly on your page
- You have a broken version of jQuery. This may be due to someone editing the main file or the plugin could overwrite the $ variable.
- You have JavaScript running until the page is fully loaded and jQuery is fully loaded.
Make sure all JS code is in this:
$(document).ready(function () {
Change the save / send / send button exception
$('#save').click(function() { unsaved = false; });
Edit to work with dynamic inputs
// Another way to bind the event $(window).bind('beforeunload', function() { if(unsaved){ return "You have unsaved changes on this page. Do you want to leave this page and discard your changes or stay on this page?"; } }); // Monitor dynamic inputs $(document).on('change', ':input', function(){ //triggers change in all input fields including text type unsaved = true; });
Add the above code to your alert_unsaved_changes.js file.
Hope this helps.
AlphaMale Aug 07 2018-12-12T00: 00Z
source share