在firefox 38中,我注意到如果我激活浏览器反弹出功能,它会在调用window.alert()
或window.confirm()
例如,给定代码:
var cb = document.querySelector("input");
cb.onclick = function(){
console.log("before alert");
window.alert("foo");
console.log("after alert");
};
<center><input type=checkbox></center>
如果您单击该复选框几次,每次单击之间的间隔少于3秒,则在弹出窗口中您可以选择“阻止此页面创建其他对话框”。激活它。完成后,您将看到警报停止执行后的console.log。
这是一个预期的Firefox行为吗?它在哪里记录?
Chrome不会以这种方式运行 - 它仍会在window.alert()
行之后执行代码。
修改 - 我发现了一个错误报告:https://bugzilla.mozilla.org/show_bug.cgi?id=633154。似乎在某些版本中,如果警报被抑制,它们会抛出NS_ERROR_NOT_AVAILABLE异常,但是某些版本却没有,而且它是静默的。
答案 0 :(得分:1)
没有发现任何提到这种奇怪的警觉行为。 HTML5规范。只说一般:
...用户代理可以为用户提供忽略所有警报的选项......
我建议将警报包装到try / catch块中,使其表现为“标准”:https://jsfiddle.net/zvtam8ur/6/
function myAlert(message) {
try {
window.alert(message);
} catch (e) {
console.log("alert error!");
}
}
var cb = document.querySelector("input");
cb.onclick = function(){
console.log("before alert");
myAlert("foo");
console.log("after alert");
};
答案 1 :(得分:1)
我曾在Firefox和Google Chrome中尝试过。
那里没有这样的功能。
甚至没有确认(这里给出的例子) -
function myFunction() {
var txt;
console.log("before alert");
var r = confirm("Press a button!");
if (r == true) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
console.log("after alert");
document.getElementById("demo").innerHTML = txt;
}
<p>Click the button to display a confirm box and check your console log.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
当然,正常的警报也不允许在Firefox和Chrome中使用 -
function myFunction() {
console.log("before alert");
alert("Hello! I am an alert box!");
console.log("after alert");
}
<p>Click the button to display an alert box and check the console log.</p>
<button onclick="myFunction()">Try it</button>
我在Firefox和Chrome中的测试结果在this youtube video中给出。
为了更清楚地看到,请改变视频的速度 -
希望,你有完整的答案。