ホスティング ページと通信する必要がある iframe を持つ Web アプリケーションがあります。iframe とホストは異なるドメインとプロトコル上にあります (iframe は https、メイン ページは http)。postMessage を使用して、外側のページから iframe への状態 (ユーザー トラッキング) の一部を取得します。
iframe が読み込まれると、短いメッセージがトップ ページに送信され、visitorid を要求されます。
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 から 1 つと giveVisitorId から 1 つ、2 つのアラートを受け取ります)。ただし、XP の IE8 では、(giveVisitorId からの) 最初のアラートしか取得しません。
postMessage の送信は機能し、受信の 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, "*");
}
},