取消来自同一页面另一部分的警报

cancel an alert that occurs from another part of the same page

本文关键字:一部分 另一部 一页 取消      更新时间:2023-09-26

我遇到了一个非常独特/奇怪的情况。在我的项目中,在我的主模板上,我在页面底部遇到了以下情况。。。。

...
<body>
<div>blah...blah</div>

/*FOR CURTAIN REASONS, I CAN'T TOUCH THIS SCRIPT*/
<script type="text/javascript">
var ccPopup = window.open('http://www.google.com','_blank'); 
if (!ccPopup) alert('Your browser has popup blocker enabled. Please disable it and try again');
var _fV4UI = true;
</script>
/**/
/*BUT I CAN CONTROL ALL THINGS BELOW...*/
<script type="text/javascript" src="jquery_1.8.3.min.js"></script>
<script type="text/javascript" src="my-javascript-file.js"></script>
/**/
</body>
..

我基本上想防止(!ccPopup)执行它的警报消息,该消息在每次加载页面时都会发生。如何防止此警报操作在my-javascript-file.js内部发生?如果我不能在那里做到这一点,如果需要的话,我还可以灵活地在页面的<head>中添加javascript…

谢谢你的建议!

如果页面上没有任何其他警报,您可能可以通过这样的操作逃脱惩罚。

  window.alert = function() {
      return false;
  }

不漂亮,但它可能有用。

在头部,您将不得不劫持警报功能:

showAlert = false;
myAlert = window.alert;
window.alert = function(msg) { 
    if (showAlert) { 
        myAlert(msg); 
    } 
}

然后,您可以通过更改showAlert的值来切换是否显示警报;

showAlert = true; alert("hi"); //alerts "hi"
showAlert = false; alert("hi"); //nothing happens.

你也可以做一些类似的事情:

window.alert = function(msg) { 
    if (msg != "Your browser has popup blocker enabled. Please disable it and try again") { 
        myAlert(msg); 
    } 
}

这绝对是一个丑陋的破解,但您可以重新定义window.open():

window.open = function() { return true; };

稍后:

var ccPopup = window.open('http://www.google.com','_blank');
if (!ccPopup)
    alert('...');

第一行应该在head中,在另一个代码段之前。

http://jsfiddle.net/0abgvrtf/