window.alert 之后的代码在其他对话框被阻止时不执行
code after window.alert not executing when additional dialog boxes prevented
在 firefox 38 中,我注意到如果我激活浏览器的反弹出功能,它会停止执行调用 window.alert()
或 window.confirm()
[=17= 之后的代码行]
例如,给定代码:
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 秒,那么在弹出窗口中您可以选择 "Prevent this page from creating additional dialogs"。激活那个。完成后,您会看到警报停止执行后的 console.log。
这是预期的 Firefox 行为吗?它在哪里记录?
Chrome 不会这样 - 它仍然执行 window.alert()
行之后的代码。
编辑 - 我发现了错误报告:https://bugzilla.mozilla.org/show_bug.cgi?id=633154。似乎在某些版本中,如果警报被抑制,它们会抛出 NS_ERROR_NOT_AVAILABLE 异常,但在某些版本中它们不会,并且它是无声的。
未发现任何提及此类奇怪警报行为的信息。 HTML5 规格。只说一般:
... the user agent might give the user the option to ignore all alerts ...
我建议将警报包装到 try/catch 块中以使其正常运行 "standard": 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");
};
我已经在 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 中给出。
为了看得更清楚,请像这样改变视频的速度-
希望您有完整的答案。
在 firefox 38 中,我注意到如果我激活浏览器的反弹出功能,它会停止执行调用 window.alert()
或 window.confirm()
[=17= 之后的代码行]
例如,给定代码:
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 秒,那么在弹出窗口中您可以选择 "Prevent this page from creating additional dialogs"。激活那个。完成后,您会看到警报停止执行后的 console.log。
这是预期的 Firefox 行为吗?它在哪里记录?
Chrome 不会这样 - 它仍然执行 window.alert()
行之后的代码。
编辑 - 我发现了错误报告:https://bugzilla.mozilla.org/show_bug.cgi?id=633154。似乎在某些版本中,如果警报被抑制,它们会抛出 NS_ERROR_NOT_AVAILABLE 异常,但在某些版本中它们不会,并且它是无声的。
未发现任何提及此类奇怪警报行为的信息。 HTML5 规格。只说一般:
... the user agent might give the user the option to ignore all alerts ...
我建议将警报包装到 try/catch 块中以使其正常运行 "standard": 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");
};
我已经在 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 中给出。
为了看得更清楚,请像这样改变视频的速度-
希望您有完整的答案。