Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IE11中的后消息处理_Javascript_Internet Explorer 11_Postmessage - Fatal编程技术网

Javascript IE11中的后消息处理

Javascript IE11中的后消息处理,javascript,internet-explorer-11,postmessage,Javascript,Internet Explorer 11,Postmessage,假设我有三个URL: 一, 三, window.addEventListener('message',函数(事件){ document.getElementById('h4')。innerHTML=event.origin; }); 现在,在任何“普通”浏览器(edge/firefox/chrome)中,加载-链将从“”加载脚本到一个iframe中,该iframe将加载另一个带有“”的iframe,最后一个iframe将显示“”作为事件源。但是,在internet explorer 11中,

假设我有三个URL:

一,

三,


window.addEventListener('message',函数(事件){
document.getElementById('h4')。innerHTML=event.origin;
});

现在,在任何“普通”浏览器(edge/firefox/chrome)中,加载-链将从“”加载脚本到一个iframe中,该iframe将加载另一个带有“”的iframe,最后一个iframe将显示“”作为事件源。但是,在internet explorer 11中,事件来源设置为“关于:”。有没有办法避开这个特殊的怪癖?我还没有找到任何关于这个特殊怪癖的文档(参考:)

我做了一个测试,重现了同样的问题。我也搜索了很多信息,但没有找到任何线索。我猜如果使用两个iframe会导致问题。因此,我只使用一个iframe和
事件进行另一个测试。origin
可以在IE中获得正确的url。我使用如下代码:

  • 
    window.addEventListener('message',函数(事件){
    document.getElementById('h4')。innerHTML=event.origin;
    });
    
  • 虽然找不到任何文档证明,但测试结果让我认为IE在使用嵌入iFrame时可能存在一些限制。作为一种变通方法,您可以尝试只使用一个iframe

    <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>
    
    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);
    
    <html>
    <head>
    <script>
      window.addEventListener('message', function(event) {
        document.getElementById('h4').innerHTML = event.origin;
      });
    </script>
    </head>
    <body>
      <h4 id="h4"></h4>
    </body>
    </html>
    
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title></title>
    </head>
    <body id="i1">
        <script>
           var contentWindow = document.getElementById('i1').contentWindow;
           var oScript = document.createElement('script');
           oScript.src = "http://loader.com";
           document.childNodes[1].childNodes[2].appendChild(oScript);
        </script>
    </body>
    </html>
    
    var oFrame = document.createElement("iframe");
    oFrame.src = "http://sf.com";
    oFrame.onload = function () {
        oFrame.contentWindow.postMessage({ msg: 'hi' }, 'http://sf.com');
    }
    document.childNodes[1].childNodes[2].appendChild(oFrame);
    
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title></title>
        <script>
           window.addEventListener('message', function(event) {
               document.getElementById('h4').innerHTML = event.origin;
           });
        </script>
    </head>
    <body>
       <h4 id="h4"></h4>
    </body>
    </html>