I have a test form that displays a warning message when the user attempts to leave a form with unsaved data.
I used jquery onbeforeunload
.
The issue I have is that on the form cancel buttton there is a showProgressAnimation()
call to display a please wait icon while the page does its thing. I have attached the showProgressAnimation()
call to the cancel button and this is triggered whenever the cancel button is called - even when the user request to stay on the form.
How do I trigger call the showProgressAnimation()
when the user clicks on the "Leave this Page' button of the pop up generated by the onbeforeunload
but is also called / triggerd when the form is correctly submitted (when the 'Leave this Page' pop up is not called)?
Here is my form code:
<form id="achievement_details_form" class="form-horizontal" method="post">
.........culled for brevity..........
<a href="{% url 'achievement_details' %}" class="btn" onclick="showProgressAnimation();">Cancel</a>
</form>
Here is my form script:
<script>
function showProgressAnimation() {
$("#loading-div-background").show();
}
window.onbeforeunload = function(){
return 'You have unsaved changes!';
};
</script>