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

标签 iframe sandbox allow-modals

从Chrome 46开始,需要在iframe的sandbox属性中添加“允许模式”标志,以允许模式(例如警报和确认)脱离iframe。到目前为止没有问题。

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

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

最佳答案

似乎唯一添加它的方法是通过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>





似乎很奇怪,新的属性值无法在较旧的浏览器中使用。向后不兼容的更改不是网络应该工作的方式。 :/

关于iframe - 如何在尚不支持iframe的浏览器中处理“allow-modals”沙箱标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33494074/

相关文章:

html - 无法在 iframe 中隐藏滚动条?

haskell - Safe Haskell 的安全性如何?

testing - PayPal Sandbox 交易不显示给卖家

java - 将桌面应用程序移植到 Applet 沙箱

office-js - Outlook 加载项停止工作,因为 Outlook Web 界面想要使用被浏览器阻止的对话框确认域

javascript - href ="javascript:..."语法突然在 FireFox 的 FB Iframe 中不起作用

javascript - jQuery 从 iframe 弹出图像

javascript - 如何将iframe使用的网页居中