Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating OnBeforeUnload ONLY when field values have changed

Tags:

What I'm trying to achieve is to Warn the user of unsaved changes if he/she tries to close a page or navigate away from it without saving first.

I've managed to get the OnBeforeUnload() dialog to pop-up... but I don't want it to be displayed at all if the user hasn't modified any field values. For this, I'm using this hidden input field called is_modified that starts with a default value of false and flips to true when any field is edited.

I tried to bind the change event to this is_modified field to try and detect for value change... and only then activate OnBeforeUnload.

$( '#is_modified' ).change( function() {     if( $( '#is_modified' ).val() == 'true' )         window.onbeforeunload = function() { return "You have unsaved changes."; } }); 

But from what I figure is that the change() event works only after these 3 steps - a field receives focus, a value is changed and the field looses focus. In case of the hidden input field, I'm not sure how this receiving and loosing focus part works! Hence, the onbeforeunload function is never being activated.

Can anyone suggest a way to maintain a trigger over is_modified?

Thanks.

like image 587
miCRoSCoPiC_eaRthLinG Avatar asked May 29 '09 09:05

miCRoSCoPiC_eaRthLinG


People also ask

What triggers Onbeforeunload?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.

How do you cancel Windows Onbeforeunload?

Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.

What Javascript event do we use to make the user confirm before leaving the page?

If you want to show confirmation dialog before user leaves or reloads the page, you can use Javascript onbeforeunload() event attribute. When unload event fires, it shows a prompt asking whether user wants to leave or reload the page.

What is Onbeforeunload in JS?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.


2 Answers

I had a similar requirement so came up with following jQuery script:

$(document).ready(function() {     needToConfirm = false;      window.onbeforeunload = askConfirm; });  function askConfirm() {     if (needToConfirm) {         // Put your custom message here          return "Your unsaved data will be lost.";      } }  $("select,input,textarea").change(function() {     needToConfirm = true; }); 

The above code checks the needToConfirm variable, if its true then it will display warning message. Whenever input, select or textarea elements value is changed, needToConfirm variable is set to true.


PS: Firefox > 4 don't allow custom message for onbeforeunload.
Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=588292


UPDATE: If you are a performance freak, you will love @KyleMit's suggestion. He wrote a jQuery extension only() which will be executed only once for any element.

$.fn.only = function (events, callback) {     //The handler is executed at most once for all elements for all event types.     var $this = $(this).on(events, myCallback);     function myCallback(e) {         $this.off(events, myCallback);         callback.call(this, e);     }     return this };      $(":input").only('change', function() {     needToConfirm = true; }); 
like image 155
Ajinkya Avatar answered Oct 02 '22 22:10

Ajinkya


We just use Window.onbeforeunload as our "changed" flag. Here's what we're doing, (using lowpro):

Event.addBehavior({   "input[type=radio]:change,input[type=text]:change,input[type=checkbox]:change,select:change": function(ev) {        window.onbeforeunload = confirmLeave;   }   ".button.submit-button:click": function(ev) {        window.onbeforeunload = null;   }, });   function confirmLeave(){     return "Changes to this form have not been saved. If you leave, your changes will be lost."   } 
like image 23
Bryan Larsen Avatar answered Oct 02 '22 23:10

Bryan Larsen