Javascript alert() 在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6184169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:37:20  来源:igfitidea点击:

alert() not working in Chrome

javascriptgoogle-chromealert

提问by anonymous coward

error

错误

'nuff said. I have absolutely no clue why using alert() there wouldn't work. It works perfectly in Firefox, but gives that error in Chrome.

'纳夫说。我完全不知道为什么使用 alert() 不起作用。它在 Firefox 中完美运行,但在 Chrome 中会出现该错误。

回答by Matty F

window.alert = null;
alert('test'); // fail
delete window.alert; // true
alert('test'); // win

windowis an instance of DOMWindow, and by setting something to window.alert, the correct implementation is being "shadowed", i.e. when accessing alertit is first looking for it on the windowobject. Usually this is not found, and it then goes up the prototype chain to find the native implementation. However, when manually adding the alertproperty to windowit finds it straight away and does not need to go up the prototype chain. Using delete window.alertyou can remove the window own property and again expose the prototype implementation of alert. This may help explain:

window是 的一个实例DOMWindow,并且通过将某些内容设置为window.alert,正确的实现被“隐藏”,即在访问alert它时首先在window对象上寻找它。通常这是找不到的,然后它会沿着原型链向上查找本机实现。但是,当手动alert向其添加属性时,window它会立即找到它,不需要沿着原型链向上。使用delete window.alert您可以删除窗口自己的属性并再次公开alert. 这可能有助于解释:

window.hasOwnProperty('alert'); // false
window.alert = null;
window.hasOwnProperty('alert'); // true
delete window.alert;
window.hasOwnProperty('alert'); // false

回答by whitebeard

I had the same issue recently on my test server. After searching for reasons this might be happening and testing the solutions I found here, I recalled that I had clicked the "Stop this page from creating pop-ups" option a few hours before when the script I was working on was wildly popping up alerts.

我最近在我的测试服务器上遇到了同样的问题。在搜索了可能发生这种情况的原因并测试了我在此处找到的解决方案后,我记得我在几个小时前单击了“停止此页面创建弹出窗口”选项,当时我正在处理的脚本疯狂弹出警报.

The solution was as simple as closing the tab and opening a fresh one!

解决方案就像关闭标签并打开一个新标签一样简单!

回答by tofutim

Take a look at this thread: http://code.google.com/p/chromium/issues/detail?id=4158

看看这个线程:http: //code.google.com/p/chromium/issues/detail?id=4158

The problem is caused by javascript method "window.open(URL, windowName[, windowFeatures])". If the 3rd parameter windowFeatures is specified, then alert box doesn't work in the popup constrained window in Chrome, here is a simplified reduction:

http://go/reductions/4158/test-home-constrained.html

If the 3rd parameter windowFeatures is ignored, then alert box works in the popup in Chrome(the popup is actually opened as a new tab in Chrome), like this:

http://go/reductions/4158/test-home-newtab.html

it doesn't happen in IE7, Firefox3 or Safari3, it's a chrome specific issue.

See also attachments for simplified reductions

该问题是由 javascript 方法“window.open(URL, windowName[, windowFeatures])”引起的。如果指定了第三个参数 windowFeatures,则警告框在 Chrome 的弹出约束窗口中不起作用,这里是一个简化的减少:

http://go/reductions/4158/test-home-constrained.html

如果第三个参数 windowFeatures 被忽略,则警告框在 Chrome 的弹出窗口中起作用(弹出窗口实际上在 Chrome 中作为新选项卡打开),如下所示:

http://go/reductions/4158/test-home-newtab.html

它不会发生在 IE7、Firefox3 或 Safari3 中,这是一个 chrome 特定问题。

另请参阅附件以了解简化的减少

回答by Alpha G33k

Here is a snippet that does not need ajQuery and will enable alerts in a disabled iframe (like on codepen)

这是一个不需要 jQuery 的片段,它将在禁用的 iframe 中启用警报(如在 codepen 上)

for (var i = 0; i < document.getElementsByTagName('iframe').length; i++) {
    document.getElementsByTagName('iframe')[i].setAttribute('sandbox','allow-modals');
}

Here is a codepen demo working with an alert() after this fix as well: http://codepen.io/nicholasabrams/pen/vNpoBr?editors=001

这是在此修复后使用 alert() 的 codepen 演示:http://codepen.io/nicholasabrams/pen/vNpoBr?editors=001

回答by RobertoFRey

put this line at the end of the body. May be the DOM is not ready yet at the moment this line is read by compiler.

把这条线放在身体的末端。可能在编译器读取这一行时 DOM 还没有准备好。

<script type="text/javascript" src="script.js"></script>"