2014-09-29 90 views
0

我有以下代码获取子IFRAME SRC值

我想获得孩子的src值iframe.I我无法得到那个value.I甚至使用

document.getElementById("abcd") also.But没有运气...

通过使用唯一的JavaScript我需要的解决方案。

在此先感谢...

+0

您需要访问相对于iFrame的DOM。看看http://stackoverflow.com/questions/1654017/how-to-expose-iframes-dom-using-jquery – 2014-09-29 12:16:45

回答

1
`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/'; 

如果用户在iframe内的导航,你不能访问使用src中的URL的值。在前面的例子,如果用户在www.google.com上消失了,你做的事:

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

你仍然会得到“http://www.google.com”。

documentWindow.location.href仅当iframe包含与包含窗口相同的域中的页面时才可用,但如果可用,它始终包含URL的正确值,即使用户在iframe中导航。

如果您尝试访问(documentWindow下或任何东西)documentWindow.location.href和iframe是在不属于包含窗口的域页面时,它会抛出一个异常:

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

尝试比较.src.documentWindow.location.href的值为iframe。 (注意:documentWindow在Chrome中被称为contentDocument,因此Chrome中的.documentWindow.location.href将代替.contentDocument.location.href。)