2012-06-11 3 views
0

I가 아래의 내용과 iframe이 설정이있는 페이지, 인덱스 : 이제은 iframe은 그들 사이에 통신

<div class="hidden" id="hiddenStuff"> 
.... 
</div> 

:

인덱스 페이지 :

<html> 
<body> 

<iframe src=".." id="leftFrame"></frame> 
<iframe src="mainFrame.html" id="mainFrame"></frame> 

</body> 

</html> 

mainFrame.html을 mainFrame에서 숨겨진 div에 일부 내용을로드하고 있습니다.

mainFrame.html에서 해당 내용을 가져 와서을 통해 mainFrame에서 시작되는 색인에 표시 할 수 있습니까?

즉 mainFrame이로드되면 자바 스크립트를 통해 부모 iframe으로 호출하여 #hiddenStuff의 내용을 가져온 다음 색인 페이지에 표시합니다.

답변

1

예 부모 및 메인 프레임이 모두 동일한 도메인에있는 경우 가능합니다.

부모/인덱스 :

<html> 
<body> 

<script type="text/javascript"> 

function updateContent(content) 
{ 
    $('#content').html(content); 
} 

document.domain = "yourdomain.com"; 

</script> 

<iframe src=".." id="leftFrame"></frame> 
<iframe src="mainFrame.html" id="mainFrame"></frame> 

<div id="content"> </div> 

</body> 

</html> 

메인 프레임/어린이 : 요소로 원시 HTML을 다룰 때

<script type="text/javascript"> 

document.domain = "yourdomain.com"; 

$(document).ready(function(){ 

    // on page load, trigger the update 
    var content = $('#hiddenStuff').html(); 

    try 
    { 
     parent.updateContent(content); 
    } 
    catch(e) 
    { 
     // couldn't call the parent, handle the exception 
    } 

}); 

</script> 

<div class="hidden" id="hiddenStuff"> 
.... 
</div> 

당신이 XSS의 가능성이있는 경우를 고려할 필요가 명심 콘텐츠는 사용자 입력에서 비롯됩니다.

관련 문제