Reputation: 23
Following the question and the answer on Create confirm JavaScript for when users try to leave page without saving the solution from MarkR
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Please click Update. Unsaved changes will be lost.";
}
</script>
works fine - except it asks me to confirm even if I click on the "Submit" button. I understand why. My question though is how to skip the execution of the code if the Submit button is used? Thanks.
Upvotes: 1
Views: 880
Reputation: 2804
I would suggest to have additional value submitted
that will be set to true
in case user pressed "Submit", but will reset to false
when user changed any input on the page. That really depends on the type of application you're developing.
I assume you have input
fields of different type
, I would do
$('input').change(function(e) {
window.submitted = false;
})
And then check window.submitted
value in your confirmExit
function.
Upvotes: 0
Reputation: 780798
Remove the beforeunload
handler when the user submits the form.
document.getElementById("formid").addEventListener("submit", function() {
window.onbeforeunload = null;
});
Upvotes: 2