如何在尚不支持iframe的浏览器中处理iframe的'allow-modals'沙箱标志?

Bas*_*ter 17 iframe sandbox allow-modals

从Chrome 46开始,需要在iframe的沙箱属性中添加"allow-modals"标志,以允许模式(如alert和confirm)突破iframe.到目前为止没问题.

但是当您在尚未支持该标志的浏览器中运行该代码时(如版本46之前的Safari或Chrome),您会收到以下错误: 解析'sandbox'属性时出错:'allow-modals'是无效的沙盒标志.

有人知道如何在没有某种浏览器嗅探的情况下解决这个问题吗?

Nic*_*bal 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');
  }
}
Run Code Online (Sandbox Code Playgroud)
<button onclick='allowModals()' style='display: block'>Allow modals</button>
<iframe src="//example.com"></iframe>
Run Code Online (Sandbox Code Playgroud)

新的属性值不能在旧浏览器中使用,这似乎非常奇怪。向后不兼容的更改不是网络应有的工作方式。:/