postMessage在跨域窗口之间不工作在IE10(它适用于帧) [英] postMessage between cross-domain windows not working in IE10 (it works for frames)

查看:168
本文介绍了postMessage在跨域窗口之间不工作在IE10(它适用于帧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循本教程 http://davidwalsh.name/window-postmessage ,并创建了跨域消息脚本在Chrome和Firefox,但不是在IE 10.工作。任何人都可以给我一些命中,如何修改它为IE 8 +?

I followed this tutorial http://davidwalsh.name/window-postmessage, and created cross domain messaging scripts which works in Chrome and Firefox but not in IE 10. Could anyone give me some hits on how to modify it for IE 8+?

在一个服务器:192.168.15.223) - receiver

At one server(eg: 192.168.15.223)--receiver

<script>
//listener
window.addEventListener('message',function(event) {
    if(event.origin !== 'http://120.0.0.211') return;
    document.getElementById('cc').innerHTML = event.data;
},false);

window.attachEvent('onmessage',function(event) {
    if(event.origin !== 'http://120.0.0.211') return;
    document.getElementById('cc').innerHTML = event.data;
},false);
</script>
<p>At 192.18.15.223 server</p>
<div id='cc'>Nothing received yet</div>

在另一台服务器上(例如:120.0.0.211) - sender

At another server(eg: 120.0.0.211)--sender

<script>
//create popup window
var domain = 'http://192.18.15.223';
var myPopup = window.open(domain + '/receiver','myWindow','width=400,height=200');
//message sender
function popup(){
    var message = 'A message sent from 120.0.0.211:';
    myPopup.postMessage(message,domain); //send the message and target URI 
}
</script>
<div id="bb">At 120.0.0.211 server</div>
<button type="button" onclick="popup()">send the message!</button>

以上脚本在Chrome和Firefox中完美工作,窗口弹出并且可以接收消息, (8+)它只弹出窗口,但没有收到消息(或可能不能发送)。

Above scripts work perfectly in Chrome and Firefox, window pops up and message can be received, however in IE(8+)it only pops the window but message is not received(or may be can't send).

我的主要目的是使两个域发送和接收简单数据(文本,单张照片等),并且不包括在后端的太多变化。所以不考虑webservice。

My main purpose is making two domains send and receive simple data (texts, single photo etc), and not including too much change on the backend. So webservice is not considered.

任何帮助将不胜感激。

这里是一些可能有助于调查问题的链接。 / p>

Here is some links that might be helpful to investigate the problems.


  1. 这个帖子建议在IE上使用attachEvent,我已经在上面的代码中:
    在互联网浏览器中添加EventListener

此微软正式文档显示IE 8+应该支持addEventListener:
http://msdn.microsoft.com/en-us/library/ie/cc197057(v = vs.85).aspx

This microsoft officially document shows IE 8+ should support addEventListener: http://msdn.microsoft.com/en-us/library/ie/cc197057(v=vs.85).aspx

建议使用Jquery bind()取代addEventListener:
jQuery equivalent JavaScript的addEventListener方法

This recommend using Jquery bind() to replace addEventListener: jQuery equivalent of JavaScript's addEventListener method


推荐答案

IE不支持postMessage跨域弹出窗口(例如:window.open)。 IE不支持嵌入帧的postMessage(例如:top.frames)。

IE does not support postMessage between cross-domain popup windows(eg:window.open). IE does support postMessage for embedded frames(eg:top.frames).

所以我最终在一个对话框中放置一个框架,假装像一个弹出窗口。例如:

So I end up by putting a frame into a dialog, pretending like a popup windows. eg:

在Jquery UI对话框的帮助下

With the help of Jquery UI dialog

<script>
$("#dialog").dialog({
    autoOpen: false,
    modal: true,
    height: 300,
weight: 400,
});

function openiframe(){
   $('#dialog').dialog('open');
});
</script>

<p>At 120.0.0.211 server</p>
<button type="button" onclick="openiframe()">send the message!</button>
<div id="dialog">
   <iframe id="iframe" src="http://192.168.15.223/smallframe"></iframe>
</div>

可能是其他解决方案/技术用于跨域窗口之间的换向:

It might be other solutions/technologies out there for commutation between cross-domain windows:


  1. Cross - 使用Ajax的资源共享(CORS)

使用像REST这样的Webservice,这实际上是一个服务器到服务器的换向,而不是一个server-broswer-server结构。但它是一种如何我们可以发送一些消息到另一个服务器的方式。对于某些框架,很容易设置REST,例如:cakephp

Using Webservice like REST, which actually is a server-to-server commutation, not a server-broswer-server struct anymore. But it is a way how we can send some message to another server. For some frameworks it is easy to setup REST eg: cakephp

这篇关于postMessage在跨域窗口之间不工作在IE10(它适用于帧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆