2014-09-29 89 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。)