데이터베이스를 변경하고 부모 창의 텍스트 상자를 업데이트해야하는 두 번째 탭의 대소 문자에 다른 iFrame이있는 TabContainer가 포함 된 colorbox 상위 페이지가 있습니다. 이 보안 위험을 제시하는 것처럼iframe에서 부모 창으로 변수를 전달하는 방법은 무엇입니까?
답변
불행하게도 직접 iframe이의 경계를 넘어 상호 작용할 수 없습니다.
한 가지 해결 방법은 #anchor
값 또는 ?querystring
을 포함하도록 iframe 내의 url을 변경 한 다음 부모로부터 iframe의 URL을 읽을 수 있습니다.
및 iframe의 url을 부모로부터 어떻게 읽을 수 있습니까? –
iframe의 DOM 객체에서 'contentWindow.location.href'를 사용할 수 있습니다. 예 : var TheLocation = document.getElementById ('myframe'). contentWindow.location.href; ' –
부모로부터 위치를 설정하여 양방향으로 작동하도록 할 수도 있습니다. –
postMessage
시도해 보시기 바랍니다. 자바 스크립트 API이지만 브라우저 제한이 있습니다.
학부모 페이지에 이와 같은 메시지를 게시 할 수 있습니다.
var win = document.getElementById("iframe").contentWindow
win.postMessage(
"value",
"iframe-domain"
);
그리고 은 iframe 페이지이 같은 메시지를들을 수 있습니다.
<script>
function listener(event){
if (event.origin !== "[parent-page-domain]")
return;
// operate with event.data
}
if (window.addEventListener){
addEventListener("message", listener, false)
} else {
attachEvent("onmessage", listener)
}
</script>
SOURCE - http://javascript.info/tutorial/cross-window-messaging-with-postmessage
당신이 몇 가지 코드로 이미지를 전환하십시오. – Teemu