javascript unbeforeunload/unload事件注意事项

  javascript中给window注册unbeforeunload/unload事件时,不要给事件添加alert,confirm等语句,高版本的浏览器如chrome【webkit核心】,IE9+等会拦截这些语句的执行。



来源:http://stackoverflow.com/questions/6063522/jquery-beforeunload
If you want to run a function as the page is unloading, you can use $(window).unload(), just note that it can't stop the page from unloading or redirect the user. (UPDATE: Chrome and Firefox block alerts in unload).
如果你想运行一些函数当页面被卸载的时候,你可以使用unload事件,但是注意这个事件不能阻止页面卸载或者重定向到新页面。(更新:chrome和firefox阻拦提示信息在unload事件中)

window.onunload=function(){
  alert('Bye.');
};

 


  要提示用户是否要离开页面可以使用

  window.onbeforeunload=function(){ return '确认离开页面?!';}

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:javascript unbeforeunload/unload事件注意事项

评论(0)Web开发网
阅读(540)喜欢(0)JavaScript/Ajax开发技巧