How do I get window onunload event trigger to work in Chrome v73.x?
Instead unload, use window.onbeforeunload
for Chrome and it will work fine!
window.onbeforeunload = function (e) {
console.log('It worked');
debugger;
};
Here is the event triggered in Chrome when I close the window.
I can't trigger the unload event in Chrome
window.onunload = alert("Are you sure 2?");
This is incorrect. You are setting onunload
to the result of alert
, you need to set it to a function:
window.onunload = function(){
alert("Are you sure?");
}
If you want to use jQuery, this will work in all browsers.
$(window).unload(function () {
alert("Are you sure?");
});
NOTE: It might seem like it's not working in Chrome, but it is. That's because Chrome blocks alert
s in the onunload
event.
Jquery: windows.onunload not working in Firefox & Chrome
<script>
window.onbeforeunload = function() {
return "Are you sure?";
};
</script>
or
$(window).on('beforeunload', function() {
return 'Your own message goes here...';
});
in chorme there are several blocked method on onbeforeunload like alert..
A chorme example of the debugging console for an alert:
Blocked alert('My Window is reloading') during beforeunload.
ref1 ref2
Why is jQuery unload not working in chrome and safari?
I found Joseph's comment as the correct answer, So posting this answer.
Dialogs are blocked/prevented during "beforeunload" (with exception to the beforeunload prompt) and "unload" events. Can be confirmed by checking your console.
Related Topics
Javascript: How to Pass Object by Value
Injecting a Mock into an Angularjs Service
Getting Binary Content in Node.Js Using Request
Math Random Number Without Repeating a Previous Number
Displaying a Number in Indian Format Using JavaScript
How to Define Setter/Getter on Prototype
Trigger an Event with Prototype
Onclick Event Getting Called Automatically
Testing If a Checkbox Is Checked with Jquery
Fix Node Position in D3 Force Directed Layout
Jqgrid - Maintain Check Box Selection State - Page Refresh/Redirect/Reload
Deep Copy in Es6 Using the Spread Syntax
How to Remove Only the Parent Element and Not Its Child Elements in JavaScript
Vuejs 2.0 Emit Event from Grand Child to His Grand Parent Component