Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set

Whenever running alert('something') in JSFiddle, I get the error:

Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.

in the console.

I cannot find any information on this error through Google.

How do I fix this? What is, and where can I set, the 'allow-modals' keyword?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Using web browser Developer Tools or something like Grease Monkey/Tamper Monkey change the 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:

// ==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';
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...