任何不良的副作用window.onbeforeunload返回没有价值? [英] Any bad side effects to window.onbeforeunload returning no value?

查看:218
本文介绍了任何不良的副作用window.onbeforeunload返回没有价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在Chrome和FF中,如果window.onbeforeunload返回null,那么对话框将不会弹出。但是在IE中,它弹出消息null。为了使IE不创建弹出窗口,window.onbeforeunload应该不会返回任何内容。但是在Chrome和FF中没有任何其他的副作用?如果不是的话,为什么还有人会在第一时间写'return null'呢?

例如,这样做:

  window.onbeforeunload = function(){
if(shouldNotWarnBeforeUnload)
{return null; }
else
{return('你确定要离开页面吗?'); }
返回null;
};



  window.onbeforeunload = function(){
if(shouldNotWarnBeforeUnload)
{}
else
{return('你确定要离开页面吗? ); }
};

在Chrome中有不同的表现?

解决方案在Chrome中,返回 null 相当于不返回任何内容,或者返回 undefined 。 >
当没有任何东西被返回时,对话框将不会弹出。



注意:在第一个示例中,事件侦听器的最后一行永远不会到达,因为要么返回 if else 块。



而不是返回 null undefined 等等,只是否定条件 http://jsfiddle.net/f6uTw/

  window.onbeforeunload = function(){
if(!shouldNotWarnBeforeUnload){
return'您确定要离开页面吗?
}
};


I know that in Chrome and FF, if window.onbeforeunload returns null, then the dialog box will not pop up. But in IE, it pops up with the message 'null'. In order to make IE not create a pop-up, window.onbeforeunload should return nothing. But does returning nothing have any other side effects in Chrome and FF? If not, why would anyone bother to write 'return null;' in the first place?

For example, do this:

window.onbeforeunload = function() { 
  if (shouldNotWarnBeforeUnload)
    { return null; }
  else 
    { return ('Are you sure you want to leave the page?'); }
  return null;
};

and this

window.onbeforeunload = function() { 
  if (shouldNotWarnBeforeUnload)
    {  }
  else 
    { return ('Are you sure you want to leave the page?'); }
};

behave differently in Chrome?

解决方案

In Chrome, returning null is equivalent to returning nothing or undefined.
When nothing is returned, the dialog will not pop up.

Note: In your first example, the last line of the event listener is never reached, because either the if or else block returned.

Instead of returning null, undefined etc, just negate the condition : http://jsfiddle.net/f6uTw/

window.onbeforeunload = function() { 
    if (!shouldNotWarnBeforeUnload) {
        return 'Are you sure you want to leave the page?';
    }
};

这篇关于任何不良的副作用window.onbeforeunload返回没有价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆