i'm binding a function to an event with
window.onbeforeunload = function() {
somefunction()
}
which is working on unload as planned, but if they cancel the onbeforeunload the function is still attached, is it possible to check if the user cancels onbeforeunload
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.
You can't. A page refresh is like navigating away and unloading the DOM so the onbeforeunload event will always be called.
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.
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 default message that appears in the confirmation box, is different in different browsers.
Actually, I found it was quite easy: I just set
window.onbeforeunload = null;
for each click before it was run, allowing the event handler to be run afterwards.
or you can just return null
window.onbeforeunload = function() {
return null;
};
I am using following snippet for that: window.onbeforeunload gist
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