2016-06-15 5 views
8

javascriptのpostMessageイベントでイベントの起点について何か得られないことがあります。ここでサンドボックス化されたiFrameからメインウィンドウへのPostMessage、originは常にnull

は私のメインのページです:

<html> 
<body> 

<h1>Test</h1> 
<h2>Outside</h2> 

<iframe src="iframe-include.html" 
    width="100%" height="100" 
    sandbox="allow-scripts"></iframe> 

<script type="text/javascript"> 
window.addEventListener('message', function (event) { 
    console.log(event); 
}, false); 
</script> 

</body> 
</html> 

そして、私のiFrameコンテンツコンソールを見ると

<html> 
<body> 

<h3>Inside</h3> 

<script type="text/javascript"> 
var counter = 1, 
    domain = window.location.protocol + '//' + window.location.host, 
    send = function() { 
     window.setTimeout(function() { 
      console.log('iframe says:', domain); 
      window.parent.postMessage(counter, domain); 
      counter += 1; 
      send(); 
     }, 3000); 
    }; 

send(); 
</script> 

</body> 
</html> 

、イベントオブジェクトのoriginプロパティは、ドメイン変数であっても、常にnullでありますiFrameは正しいです。

私のコンソールは言う:

すべてのドキュメントで
iframe-include.html:11 iframe says: http://127.0.0.1:8181 
iframe.html:11 MessageEvent {isTrusted: true, data: 2, origin: "null", lastEventId: "", source: Window…} 

は、それはそれはデ「メッセージ」イベントリスナの内側にevent.originに確認することが重要だと述べています。しかし、それがいつもヌルなら、それをどうやって行うのですか?

ありがとうございます

+0

[DOMWindowで 'postMessage'を実行できませんでした:提供されたターゲットの起点が受信者ウィンドウの起点( 'null')と一致しません](http://stackoverflow.com/questions/22194409/failed- to-execute-postmessage-on-domwindow-target-origin-provided-does) –

+0

いいえ、原因は同じですが、まったく別の問題です。私もそれを持っている... – Sych

答えて

1

iframeはサンドボックス化されているため、元のデータにアクセスできなくなりました。

iframe sandboxプロパティにallow-same-originを追加すると、再び機能します。

関連する問題