【问题标题】:Does IE8 have any specific restrictions on postMessage to IFrames?IE8 对 postMessage 到 IFrame 有任何特定限制吗?
【发布时间】:2012-05-02 03:19:38
【问题描述】:

我有一个带有 iframe 的 Web 应用程序,需要与其托管页面进行通信。 iframe 和主机位于不同的域和协议上(iframe 是 https,主页是 http)。我使用 postMessage 从外部页面获取一小部分状态(用户跟踪)到 iframe。

加载 iframe 时,它​​会向首页发送一条短消息以询问访问者 ID:

if ($.w.top != $.w) $.f.postMessage($.w.top, 'Get visitorId');

($.f.postMessage(w, m) 只是 postMessage 的一个包装器,如果 typeof w.postMessage === 'undefined' 则什么都不做)。在外页,我们有一个消息监听器:

// Set up event listener so that we can respond when any iframes
// inside of us ask for our visitorId
$.f.listen($.w, 'message', giveVisitorId);
function giveVisitorId(event) {
  $.w['zzzzz'] = event.source;
  if (event.data === 'Get visitorId') {
    alert('about to reply from '+window.location.href+' with visitorid, typeof event.source.postMessage is ' + typeof(event.source.postMessage));
    event.source.postMessage('visitorId=' + $.v.visitorId, '*');
  }
}

内部框架有一个为响应注册的监听器:

$.f.listen($.w, 'message', receiveVisitorId);

function receiveVisitorId(event) {
  alert('receiveVisitorId called with: ' + event.data + ' in window '+window.location.href);
  var s = event.data.split('=');
  if (s[0] === 'visitorId' && s.length === 2) {
    $.v.visitorId = s[1];
    $.w.clearTimeout(giveUp);
    rest();
  }
}

这一切都可以在 OSX 上的 chrome 和 firefox 上正常工作(加载 iframe 时,我们会收到两个警报,一个来自 receiveVisitorId,一个来自 giveVisitorId);但是在 XP 上的 IE8 上,我们只收到第一个警报(来自 giveVisitorId)。

这很奇怪,因为似乎发出的 postMessage 有效,而输入的则无效;真正令人困惑的是,如果我们转到控制台并运行zzzzz.postMessage('hi', '*'),receiveVisitorId 中的警报会按预期发生! (注意我们将 event.source 保存在 window.zzzzz 中)。

有人知道为什么会发生这种情况吗?

PS:$.w.listen 和 $.w.postMessage 的定义,供参考:

listen: function (el, ev, fn) {
  if (typeof $.w.addEventListener !== 'undefined') {
    el.addEventListener(ev, fn, false);
  }
  else if (typeof $.w.attachEvent !== 'undefined') {
    el.attachEvent('on' + ev, fn);
  }
},
postMessage: function(w, m) {
  if (typeof w.postMessage !== 'undefined') {
    w.postMessage(m, "*");
  }
},

【问题讨论】:

    标签: iframe internet-explorer-8 postmessage


    【解决方案1】:

    我们解决了。问题是我们在内部 iframe 中的 $.f.postMessage 之后调用了 $.f.listen;所以当外部窗口向内部发送消息时,尚未附加侦听器。我不知道为什么会在 IE 而不是 chrome 或 firefox 中发生这种情况,但我们只是将其归结为浏览器之间的时间差异。

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多