1

I have the following code

<iframe src="http://www.w3schools.com" id="abc">        
     <iframe src="http://www.w3schoolss.com" id="abcd">
    </iframe>
</iframe>

I want to get the src value of child iframe.I am unable to get that value.I even use

document.getElementById("abcd") also.But no luck...

By using only javascript i need the solution.

Thanks in advance...

PSR
  • 39,804
  • 41
  • 111
  • 151
  • You need to access the DOM relative to the iFrame. Take a look at http://stackoverflow.com/questions/1654017/how-to-expose-iframes-dom-using-jquery – Andre Pena Sep 29 '14 at 12:16

1 Answers1

2
`src` is always the last URL that was loaded in the iframe without user interaction. I.e., it contains the first value for the URL, or the last value you set up with Javascript from the containing window doing:

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

If the user navigates inside the iframe, you can't anymore access the value of the URL using src. In the previous example, if the user goes away from www.google.com and you do:

alert(document.getElementById("myiframe").src);

You will still get "http://www.google.com".

documentWindow.location.href is only available if the iframe contains a page in the same domain as the containing window, but if it's available it always contains the right value for the URL, even if the user navigates in the iframe.

If you try to access documentWindow.location.href (or anything under documentWindow) and the iframe is in a page that doesn't belong to the domain of the containing window, it will raise an exception:

document.getElementById("myiframe").src = 'http://www.google.com/';
alert(document.getElementById("myiframe").documentWindow.location.href);

Try to comparethe value of .src and .documentWindow.location.href in an iframe. (Note: The documentWindow is called contentDocument in Chrome, so instead of .documentWindow.location.href in Chrome it will be .contentDocument.location.href.)

Tushar Raj
  • 761
  • 6
  • 20