数行の JavaScript を使用して iframe 要素を作成し、次のようなメッセージを送信したいと思います。
function loadiframe (callback) {
var body = document.getElementsByTagName('body')[0];
var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.seamless = 'seamless';
iframe.src = 'http://localhost:3000/iframe.html';
body.appendChild(iframe);
callback();
}
loadiframe(function() {
cpframe = document.getElementById('iframe').contentWindow;
cpframe.postMessage('please get this message','http://localhost:3000');
console.log('posted');
})
そして、http://localhost:3000/iframe.html (iframe のソース) 内は次のようになります。
<!DOCTYPE html>
<html>
<head>
<title>does not work</title>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.origin == "http://localhost:3000") {
document.write(JSON.stringify(event));
document.write(typeof event);
}
}
</script>
</head>
<body>
</body>
</html>
しかし、何も起こりません...オリジンの安全チェックを使用しないようにしましたが、それでも何も起こりません...メッセージが届かないように...
私はある種の非同期の問題を抱えていますか? postMessage が消える前に iframe がロードされていることを確認しようとしました...
EDIT1:また、コンソールにエラーは表示されません...
EDIT2:Google Chrome 11とFirefox 4で試しました
前もって感謝します。