假设我有三个urls:
1.http://home.com
<html>
<body>
<iframe id="i1"></iframe>
<script>
var contentWindow = document.getElementById('i1').contentWindow;
var oScript = contentWindow.document.createElement('script');
oScript.src = "http://loader.com";
contentWindow.document.childNodes[0].childNodes[1].appendChild(oScript);
</script>
</body>
</html>2.http://loader.com
var oFrame = document.createElement("iframe");
oFrame.src = "http://www.sf.com";
oFrame.onload = function() {
oFrame.contentWindow.postMessage({ msg: 'hi'}, 'http://sf.com');
}
document.childNodes[0].childNodes[1].appendChild(oFrame);3.http://sf.com
<html>
<head>
<script>
window.addEventListener('message', function(event) {
document.getElementById('h4').innerHTML = event.origin;
});
</script>
</head>
<body>
<h4 id="h4"></h4>
</body>
</html>现在在任何“普通”浏览器(edge/火狐/铬)中加载http://home.com -链将脚本从"http://loader.com“加载到iframe中,后者将加载另一个带有"http://sf.com”的iframe,最后一个iframe将显示"http://home.com“作为事件来源。然而,在internet explorer 11中,事件源被设置为"about:“。有没有办法绕过这个特殊的怪癖呢?我还没有找到任何关于这个特殊问题的文档(参考:https://caniuse.com/#search=postMessage)
发布于 2019-10-11 15:56:51
我在我这边做了一个测试,并重现了同样的问题。我也搜索了很多信息,但没有找到任何线索。我猜如果使用两个iframe就会导致这个问题。所以我只用一个iframe做了另一个测试,event.origin可以在IE中获得正确的url。我使用如下代码:
document.childNodes1.childNodes2.appendChild(oFrame);
oFrame = document.createElement("iframe");oFrame.src = "http://sf.com";oFrame.onload = function () { oFrame.contentWindow.postMessage({ msg:'hi‘},'http://sf.com');} var
虽然没有找到任何文档证明,但测试结果让我认为IE在使用嵌入式iframes时可能存在一些限制。作为一种解决方法,您可以尝试仅使用一个iframe。
https://stackoverflow.com/questions/58329823
复制相似问题