2010-06-29 9 views
1

이 예에서는 경고가 비어 있습니다. iFrame의 위치를 ​​읽고 싶습니다. object.src를 사용하고 있지만 src 속성을 설정 한 경우에만 작동합니다. 하지만 window.open 메서드를 사용하여 iFrame의 위치를 ​​설정하고이 경우 작동하지 않습니다. 내가 뭘? 대신 window.open()를 사용자바 스크립트의 iframe 위치

<body>  
<iframe id="location" name="address" style="width:700px; height:700px;"> 
</iframe>  
<script type="text/javascript" language="javascript"> 
    window.open("http://www.google.com","address"); 
    function clickMe() 
    { 
     var src = document.getElementBy Id("location").src; 
     alert(src); 
    } 
</script> 
<input type="button" onclick="clickMe()" value="click Me"/> 
</body> 

답변

1

같은 보안 문제는 그때의 일을 자사의 src 속성을 설정하면 나는 또한 쓰기 질문에서

<script> 
var win; 
function load(link) { 
    win=window.open(link.href,link.target) 
    return win?false:true; 
} 
function tell(winName) { 
    try { 
    alert(win.location.href) 
    try { 
     var handle = window.open('',winName); 
     alert(handle.location.href) 
    } 
    catch(e) { 
     alert('oops getting location via window handle:'+e.message) 
    } 
    } 
    catch(e) { 
    alert('Ooops getting location:'+e.message) 
    } 
} 
</script> 

<iframe src="about:blank" name="if1"></iframe><br/> 
<a href="http://www.google.com" target="if1" onClick="return load(this)">Load foreign link</a><br /> 
<a href="showhref.html" target="if1" onClick="return load(this)">Load local link</a><br /> 
<a href="#" onClick="return tell('if1')">Tell</a><br /> 
3

, 왜 그냥 사용하지 않는 :

document.getElementById("location").src = "http://www.google.com"; 

지금 당신이에서 getElementById를 사용하여 위치를() 얻을 수

당신은에 의해 그렇게 할 수 있습니다 location.href 특성에 액세스합니다. 물론 보안상의 이유로 브라우저는 다른 도메인 인 경우 iframe의 DOM에 액세스하도록 허용하지 않습니다.

var oIframe = document.getElementById("location"); 
// for compatibility 
var oDoc = oIframe.contentWindow || oIframe.contentDocument; 
if (oDoc.document) { 
    oDoc = oDoc.document; 
} 
alert(oDoc.location.href); 
+0

내가 그것에 대해 알고있는 동일한 도메인에서 작동합니다 핸들을 통해 액세스하는 경우, 그러나 열린 메서드를 사용할 때 위치를 읽는 방법을 알고 싶습니다. –

+0

나는 본다. 편집을 참조하십시오. – quantumSoup

+0

와우! 해당 위치에 액세스하는 방법을 검색했습니다! 많은 다른 사람들이 src 속성을 언급했지만 그 속성을 설정하지 않았습니다. 이것은 내 상황에 맞는 해결책이었습니다! –