2016-06-15 4 views
8

자바 스크립트 postMessage 이벤트에 대한 이벤트 원점에 대한 정보가 없습니다. 여기 샌드 박스가있는 iFrame에서 기본 창으로의 PostMessage는 origin이 항상 null입니다.

내 메인 페이지입니다 : 이벤트 객체의 원래 속성은 항상 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> 

도 도메인 변수의 경우 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에 확인하는 것이 중요하다고 말한다. 그러나 그것이 언제나 null이라면 어떻게 할 것인가? iframe이 있기 때문에 도움

+0

'DOMWindow'에 'postMessage'를 실행하지 못했습니다 : 제공된 대상 원점이 수신자 창의 원점 ('null')과 일치하지 않습니다.] (http://stackoverflow.com/questions/22194409/failed- to-execute-postmessage-on-domwindow-the-target-origin-provided-does) –

+0

아니요, 원인은 동일하지만 완전히 다른 문제입니다. 나는 그것을 너무 가지고있다 ... – Sych

답변

1

에 대한

덕분에 그것의 기원 데이터에 대한 액세스를 잃은 샌드 박스입니다.

iframe 샌드 박스 속성에 allow-same-origin을 추가하면 다시 작동합니다.