javascript setAttribute iframe allowfullscreen 在 Chrome 中不起作用

标签 javascript google-chrome dom iframe leaflet

我在 iframe 中有一张 map (Chrome 67.0.3396.87)。例如

<iframe height="480px" 
src="http://brunob.github.io/leaflet.fullscreen/" 
width="450px">
</iframe>

我们使用的 CMS 从 iframe 中删除了 allowfullscreen 属性,因此我尝试使用 Javascript 将它们重新注入(inject)。

document.getElementsByTagName("iframe")[0].getAttribute("src") == "http://brunob.github.io/leaflet.fullscreen/" ? document.getElementsByTagName("iframe")[0].setAttribute("allowFullScreen", ""):null;

这会更改 DOM,但全屏按钮不起作用。但是,如果您从 iframe 和 allowfullscreen 集开始,那么它确实有效。

<iframe height="480px" 
src="http://brunob.github.io/leaflet.fullscreen/" 
width="450px" allowfullscreen>
</iframe>

如何让 DOM 中的更改注册,它应该是自动的?!

  • 更新:这适用于 IE 11
  • 更新:这适用于 Firefox 60.0.1

最佳答案

这是 Chrome 的一项功能not present in other browsers ,您必须在启用全屏按钮之前更新 iframe 内容。您可以通过先使用 iframe 加载 HTML,然后运行脚本来解决此问题:

<iframe height="480px" 
src="http://brunob.github.io/leaflet.fullscreen/" 
width="450px">
</iframe>

<script>
// add allowfullscreen attribute to the iframe
document.getElementsByTagName("iframe")[0].getAttribute("src") == "https://interact.bcs.org/tracer/1/BCS_point_map.html" ? document.getElementsByTagName("iframe")[0].setAttribute("allowFullScreen", true):null;

// refresh the iframe container
document.getElementsByTagName("iframe")[0].src = document.getElementsByTagName("iframe")[0].src
</script>

关于javascript setAttribute iframe allowfullscreen 在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875766/

相关文章:

javascript - AJAX 按钮过滤器更新代码更新(查看新代码)

javascript - 如何从特定记录访问对象中的嵌套数组?

javascript - document.getElementById().style.display = 'none' ;仅适用于 Firefox

browser - 图像加载然后消失

javascript - 如何在客户端更改 Javascript src 文件?

javascript - 基本 URI 与命名空间 URI

javascript - iOS 停止随机发送 httprequests[Titanium]

javascript - 在输入范围的光标上显示价格

javascript - 制作动画 Chrome 扩展图标?

javascript - 如何使用 phantomJS 模拟鼠标悬停在 HTML 元素上