如何在尚未支持“allow-modals”沙盒标志的浏览器中处理iframe中的该标志?(这是一个提问标题,不需要回答)

18
自从Chrome 46版本以后,需要在iframe的沙盒属性中添加 'allow-modals' 标志,以允许模态框(如警告和确认框)打破iframe。目前还没有问题。
但是,在尚未支持该标志的浏览器中运行该代码(如 Safari 或 Chrome 版本46之前的浏览器),您将收到以下错误: “解析'sandbox'属性时出错:'allow-modals'是无效的沙盒标志。”
有没有人有什么想法可以在不使用某种浏览器嗅探的情况下解决这个问题?
1个回答

2

看起来唯一的方法是通过JS进行追加。

function allowModals(){
  for (const i of document.getElementsByTagName('iframe')) {
    if (!i.sandbox.supports('allow-modals')) {
      console.warn("Your browser doesn't support the 'allow-modals' attribute :(");
      break;
    }
    if (i.sandbox.contains('allow-modals')) continue;
    console.info(i, "doesn't allow modals");
    i.sandbox.add('allow-modals');
    console.info(i, 'now allows modals');
  }
}
<button onclick='allowModals()' style='display: block'>Allow modals</button>
<iframe src="//example.com"></iframe>

新属性值无法在旧版浏览器中使用,这似乎非常奇怪。向后不兼容的更改并不是互联网应该发展的方向。:/


谢谢回复! - Bas Slagter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接