Tuesday, April 7, 2015

How do you give a warning message whenever a user leaves a window without saving ??


How do you give a warning message whenever a user leaves a window without saving ??


Here is the answer,  you have to enter the static id in all the different controls that you wish to exclude from the message…
I changed the original script from another blog to use this static id instead of the value.  It works now
You have to go into the script and add a line for every control  that you wish to exclude from the warning message. Now you will get an error message every time you leave the form after making changes except for when the excluded controls are pressed.

You can see the highlighted code below to see what I changed from the original script.


Put the following code in the HTML header of the page.

---------
<script type="text/javascript">
$(function(){
$('#wwvFlowForm').data('htmldb',{'dirty':false});
$(':input:not(:hidden,:submit,:password,:button)').live('change',function(){
   $('#wwvFlowForm').data('htmldb',{'dirty':true})
});
$(window).bind('beforeunload',function(){
  if($('#wwvFlowForm').data('htmldb').dirty){return "The changes you made to the page were not saved."}
});

/* List all buttons and controls you want to exclude warning message */
 $( "#Save").unbind('click').removeAttr('onclick').click(function(){
  $('#wwvFlowForm').data('htmldb',{'dirty':false});
  javascript:apex.submit('SAVE')
});

$( "#Submit").unbind('click').removeAttr('onclick').click(function(){
  $('#wwvFlowForm').data('htmldb',{'dirty':false});
  javascript:apex.submit('SUBMIT')
});
});
</script>

If you have problems with the buttons not being disabled,  you probably have a dynamic action in javascript.  These two javasripts can conflict with eachother.

Remove the script for the buttons that are not working and add this line to the javascript that is triggered when to the dynamic action for when button is pressed
This should fix your problem.

$('#wwvFlowForm').data('htmldb',{'dirty':false});

6 comments:

  1. I have pasted your code in page header also excluded the buttons which are not want to show this message... Why its not working for pagination button (Previous / Next)

    ReplyDelete
  2. Remove the script for the buttons that are not working and add this line to the javascript that is triggered when the button is pressed
    This should fix your problem.

    $('#wwvFlowForm').data('htmldb',{'dirty':false});

    ReplyDelete
  3. So for the Save button, you remove the code that unbinds and submits the button

    REMOVE
    $( "#Save").unbind('click').removeAttr('onclick').click(function(){
    $('#wwvFlowForm').data('htmldb',{'dirty':false});
    javascript:apex.submit('SAVE')
    });

    and add this to your Javascript Dynamic Action.:

    $('#wwvFlowForm').data('htmldb',{'dirty':false});

    ReplyDelete
  4. I have pasted this code in the page header, but it seems to be prompting the message even for Save button.

    ReplyDelete
    Replies
    1. Make sure the id for your save button = "SAVE"

      Delete
    2. Thanks! I managed to get that working.

      I have actually got another report on the same page as this form, and the report results are dynamic with selection from the drop down.

      how do we exempt this from running for change in the results of this report?

      Delete