7

iframe があり、iframe から親ウィンドウにデータを送信したいと考えています。

iframe の js コード内に、次のステートメントがあります。

window.parent.postMessage('hello', '*');

親ウィンドウの対応するメッセージ ハンドラは次のとおりです。

$(window).bind('message', function (event) {
    console.log(event.data);
    console.log(event.origin);
    console.log(event.source);
    console.log('received');
});

localhost からコードを読み込んでおり、iframe ソースも localhost から読み込まれています。私はFirefox 21でコードを実行しています。

問題は、event.data常にnullであり、event.orign and event.sourceであるということですundefined。この問題を解決するにはどうすればよいですか?

4

2 に答える 2