I did some tests in Firefox 3, comparing the .src and .documentWindow.location.href in an iframe . (Note: documentWindow is called contentDocument in Chrome, so instead of .documentWindow.location.href in Chrome it will be .contentDocument.location.href .)
src always the last URL that was loaded in the iframe without user interaction. Ie, it contains the first value for the URL, or the last value that you set using Javascript from a window containing:
document.getElementById("myiframe").src = 'http://www.google.com/';
If the user moves inside the iframe, you can no longer access the value of the URL using src. In the previous example, if the user left www.google.com and you do:
alert(document.getElementById("myiframe").src);
You will still receive 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 is available, it always contains the correct value for the URL, even if the user goes to the iframe.
If you try to access documentWindow.location.href (or something in documentWindow ) and the iframe is on a page that does not belong to the domain of the containing window, this will throw an exception:
document.getElementById("myiframe").src = 'http://www.google.com/'; alert(document.getElementById("myiframe").documentWindow.location.href); Error: Permission denied to get property Location.href
I have not tested another browser.
Hope this helps!
Joaquin Cuenca Abela Aug 07 '09 at 14:44 2009-08-07 14:44
source share