首页 > 解决方案 >

问题描述

I have a basic iframe embedding HTML content. I applied a workaround to get it to work in IE11, but doesn't work in Edge. Works great in Chrome and FireFox. Half of my users are on Edge...

<iframe
    title="My iframe should work"
    class="large"
    id="my-slide-should-work"
    src="javascript: window.frameElement.getAttribute('srcdoc');" // this fixed it for IE11
    scrolling="no"
    srcdoc='<!DOCTYPE html><html>all the goodness</html>'>
</iframe>

Edge just shows a blank space, errors out about the JS

SCRIPT5007: SCRIPT5007: Unable to get property 'getAttribute' of undefined or null reference
javascript: window.frameElement.getAttribute('srcdoc'); (1,2)

标签: iframemicrosoft-edge

解决方案


看起来window.frameElement为 NULL,因此无法使用getAttribute获取 'srcdoc' 。

以下可能是它为 Null 的可能原因。

如果窗口没有嵌入到另一个文档中,或者如果嵌入它的文档具有不同的来源(例如来自不同的域),则此值为 null。

因此,您可以尝试检查上述标准,我建议您创建一个脚本段并尝试检查window.frameElement是否为 Null。如果它不为空,则在将其值分配给 IFrame src 之前尝试使用GetAttribute 。

您需要注意的另一件事是 MS Edge 不支持srcdoc属性。所以 MS Edge 不会对该属性产生任何影响。

参考:

(1) Window.frameElement

(2) <'iframe'>:内联Frame元素


推荐阅读