I have the following js code installed on a form page to alert the user if he attempts to leave the page in the middle of filling out a form:
<script type="text/javascript">
window.onbeforeunload = function (evt) {
var message = 'Data you entered may not be saved';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
</script>
It works fine when user clicks on other links or tabs in an attempt to navigate away from the page. But the javascript message pops up even when the user clicks on the submit button to submit changes in the form.
How do I prevent this js message from popping up when the form is submitted?
On your form’s
onsubmit, set a flag to tellonbeforeunloadthat it can ignore this unload attempt.For example:
And in your onbeforeunload , you can do: