I have this little piece of code:
<script> $(window).bind('beforeunload', function() { $.ajax({ async: false, type: 'POST', url: '/something' }); }); </script>
I wonder, how could I disable this request when user hits the submit button.
Basically something like here, on SO. When your asking a question and decide to close the page, you get a warning window, but that doesn't happen when you're submitting the form.
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.
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 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.
Call unbind
using the beforeunload
event handler:
$('form#someForm').submit(function() { $(window).unbind('beforeunload'); });
To prevent the form from being submitted, add the following line:
return false;
Use
$('form').submit(function () { window.onbeforeunload = null; });
Make sure you have this before you main submit function! (if any)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With