2

In this example, alert is blank; I want to read the location of iFrame, I am using object.src for this but it work only if I set its src attribute. But I set iFrame's location using window.open method and in this case it is not working. What I do ?

<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>
roryf
30.2k17 gold badges84 silver badges105 bronze badges
asked Jun 29, 2010 at 5:19

2 Answers 2

4

Instead of using window.open(), why don't you just use:

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

Now you can get its location using getElementById()

You can do that by accessing the location.href attribute. Of course, for security reasons, browsers won't let you access the iframe's DOM if it's another domain.

var oIframe = document.getElementById("location");
// for compatibility
var oDoc = oIframe.contentWindow || oIframe.contentDocument;
if (oDoc.document) {
 oDoc = oDoc.document;
}
alert(oDoc.location.href);
answered Jun 29, 2010 at 5:29
Sign up to request clarification or add additional context in comments.

2 Comments

I know about it, In question I also write if I set its src attribute then its works, But I want to know the way how to read the location when we use open method ?
Wow! Been searching how to access that location! Many others mentioned the src attribute but I don't set that attribute. This was the correct solution in my situation!
2

Same security issue if you access via the handle which should work in the same domain

<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 />
answered Jun 29, 2010 at 9:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.