Window.Onunload Is Not Working Properly in Chrome Browser. Can Any One Help Me

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.
Sample Image

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 alerts 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



Leave a reply



Submit