开发者

window unload method in javascript

开发者 https://www.devze.com 2023-02-02 06:10 出处:网络
Why doesn\'t this code work? $(window).unload(function(){ window.location = \'http://www.google.com/\'开发者_如何学C;

Why doesn't this code work?

$(window).unload(function(){
    window.location = 'http://www.google.com/'开发者_如何学C;
});


Firefox and Chrome do not allow a location change after the unload or beforeunload events have fired.

So your code might work in IE and Opera, but will (what I assume to be good behavior) fail in the other two Browser that I've named.


href is not present in you redirection

 window.location.href = 'http://www.google.com/';


Note that the unload event is called when the user moves away from the page (for any reason: he can close the browser, click on a link, insert a new url, click 'back'...) and should be used only for messages or confirmations (like 'Are you sure?') or any actions that do not interfere with user choice.

See jQuery documentation for some suggestions.

0

精彩评论

暂无评论...
验证码 换一张
取 消