javascript 忽略对“alert()”的调用。文档被沙箱化,并且没有设置 'allow-modals' 关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32119446/
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
Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set
提问by ????
Whenever running alert('something')
in JSFiddle, I get the error:
每当alert('something')
在 JSFiddle 中运行时,我都会收到错误消息:
Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.
忽略对“alert()”的调用。该文档已被沙盒化,并且未设置 'allow-modals' 关键字。
in the console.
在控制台中。
I cannot find any information on this error through Google.
我无法通过 Google 找到有关此错误的任何信息。
How do I fix this? What is, and where can I set, the 'allow-modals' keyword?
我该如何解决?'allow-modals' 关键字是什么,我可以在哪里设置?
回答by pierce.jason
IFrame sandboxing is a technique that helps protect from outside content creating confusing popups that would appear to come from the main website. To allow alert popups you will need to find the iframe tag, and modify the sandbox attribute to contain the allow-modals value. For JSFiddle this is the iframe named "result". You will need to Re-Run (ctrl-enter) your Fiddle after modifying the tag.
IFrame 沙盒技术有助于防止外部内容创建看似来自主网站的令人困惑的弹出窗口。要允许弹出警报,您需要找到 iframe 标记,并修改 sandbox 属性以包含 allow-modals 值。对于 JSFiddle,这是名为“result”的 iframe。修改标签后,您需要重新运行(ctrl-enter)您的 Fiddle。
Using web browser Developer Tools or something like Grease Monkey/Tamper Monkey change the iframe.
使用 Web 浏览器开发工具或类似 Grease Monkey/Tamper Monkey 的工具更改 iframe。
From this:
由此:
<iframe name="result" sandbox="allow-forms allow-popups allow-scripts allow-same-origin" frameborder="0">
To this:
对此:
<iframe name="result" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" frameborder="0">
The following TamperMonkey snippet seems to do the trick nicely:
下面的 TamperMonkey 片段似乎很好地完成了这个技巧:
// ==UserScript==
// @name Enable alert()s
// @match //jsfiddle.com/*
// @require http://code.jquery.com/jquery-latest.min.js
// @grant unsafeWindow
// ==/UserScript==
this.$ = this.jQuery = jQuery.noConflict(true);
$("iframe[name='result']").each(function() {
this.sandbox += ' allow-modals';
});
回答by Daniel A. White
That is something JSFiddle must have changed to its iframe
s to add the sandbox
attribute. Or Chrome must have added support allow-modals
.
这是 JSFiddle 必须更改为它的iframe
s 才能添加sandbox
属性的东西。或者 Chrome 必须添加了支持allow-modals
。
Actually it is something new for Chrome 46+:
实际上,它是 Chrome 46+ 的新功能: