简体   繁体   中英

Why is jQuery unload not working in chrome and safari?

unload function in jQuery works fine in Firefox but not in chrome and safari. please check this fiddle in chrome and Firefox. http://jsfiddle.net/jeevankk/Gywnw/2/ . Alerts a message when the page is refreshed.

$(window).unload(function() {  
        alert("Unload");  
});​  

This should work to show a confirmation when the users leaves, this is also not part of any standard.

$(window).on('beforeunload ',function() {
    return 'Are you sure ?';
});

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.

This is because the unload event is not part of any standard

https://developer.mozilla.org/en/DOM/window.onunload

check the bottom of the page i just linked to.

jQuery's unload works well in chrome too, with the exception that Chrome doesn't allow alerts within it. I've used it to set cookies. And if it works with Chrome hope it works in Safari too.

the unload function of jquery has some problem with browsers..refer the following link http://bugs.jquery.com/ticket/5538

can you elaborate on the problem so that we can find some work around??

你可以在身体上使用onfocusout ..但我不建议如果你试图使用类似警报的东西,在这个操作上,要求用户不要离开你的页面..

" refresh " action in the Firefox does not fire the unload event.

We should use onbeforeunload instead.

Confirm with Firefox version 47, Mac OS X

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM