javascript - 通过 jQUery 创建沙盒 iFrame

标签 javascript jquery html iframe sandbox

我正在尝试使用 jQuery 创建一个没有任何权限的 iFrame。 我可以添加 iFrame 属性,但它需要一个值。我通过添加空值(“”)来使用肮脏的解决方案,但这是正确的方法吗? 有没有更好的方法来创建无需许可的沙盒 iFrame?

$(function() {
  $('article').each(createIFrame);
});

function createIFrame() {
  setArticleContent($(this), 'hello sandbox');
}

function setArticleContent(article, content) {
  article.find('span>p').first().text('');
  $('<iframe>', {
    srcdoc: '<p>' + content + '</p>',
    frameborder: '0',
    sandbox: ' ',
    style: 'background:#00FF00',
  }).appendTo(article.find('span>p').first());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article>
  <span>
        <p>helloworld</p>
      </span>
</article>

最佳答案

I am using a dirty solution by adding an empty value (' ') but is this the right way?

它根本不脏,但是您应该注意,根据规范,您应该使用空字符串:

The value of the attribute can either be an empty string (all the restrictions are applied), or a space-separated list of tokens that lift particular restrictions

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox

$('<iframe>', {
  srcdoc: '<p>' + content + '</p>',
  frameborder: '0',
  sandbox: '', // note the space was removed
  style: 'background: #00FF00',
}).appendTo(article.find('span>p').first());

理想情况下,样式也应该通过外部样式表设置。

关于javascript - 通过 jQUery 创建沙盒 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43756993/

相关文章:

html - 减小浏览器窗口大小时容器上的 Foundation 4 自动高度

php - super 奇怪的 HTML 搞砸了

javascript - 在 JavaScript 中获取字体指标?

jquery - 如何在jquery验证中使用isNumeric?

javascript - 如何使用谷歌应用程序脚本调用 SOAP 网络服务

javascript:无法删除数组的某些元素

javascript - 如何重新创建中高亮功能?

javascript - 如何使用 JavaScript 检查元素在视口(viewport)中是否半可见?

javascript - 如何为我的 php Web 应用程序提供桌面通知?

javascript - Chromium Embedded Framework 原生函数的后台进程