如果將name
指定爲iframe
,大多數瀏覽器將允許您通過name
值訪問iframe
的window
對象。這與iframe
的id
屬性有所不同,該屬性將爲您提供iframe
元素本身(來自其所有者document
)的引用,而不是iframe
的內容window
。
簡單的例子:(從父文檔)
<iframe name='iframe1Name' id='iframe1Id'></iframe>
<script>
// option 1: get a reference to the iframe element:
var iframeElem = document.getElementById('iframe1Id');
// update the element's src:
iframeElem.src = "page.html";
// option 2: get a reference to the iframe's window object:
var iframeWindow = window.iframe1Name;
// update the iframe's location:
iframeWindow.location.href = "page.html";
</script>
讓我們來看看你的代碼:
parent.document.getElementById(id).src = parent.document.getElementById(id).src;
如果從iframe
內執行,只要你使用了正確的id
這工作。您可能希望使用調試器來驗證parent.document.getElementById(id)
是否返回對正確元素的引用,並檢查控制檯以查看是否有任何異常正在拋出(請嘗試按F12)。由於您沒有發佈完整的代碼(包括標記),我無法想出這個問題的原因。
調試提示:
- 檢查
parent.location.href
以確保您正在訪問你以爲你是窗口,
- 檢查
parent.document.getElementId(id)
,以確保你得到一個元素對象(而不是null
或undefined
) ,
- 檢查
parent.document.getElementById(id).tagName
,以確保您使用的是正確的ID(tagName
應該=== 「IFRAME」)
這條線:
parent.getElementById(id).location.reload();
有兩個問題:
getElementById()
是document
的函數,但它正在從parent
其爲window
對象調用,並且
location
是window
對象的屬性。您正嘗試訪問iframe
元素的location
屬性,該屬性不存在。您需要參考iframe
的window
,而不是其元素。
除了name
方法,還有其他方法來獲得iframe
的窗口對象:
document.getElementById('iframeId').contentWindow; // for supported browsers
window.frames["iframeName"]; // assumes name property was set on the iframe
window.frames[i]; // where i is the ordinal for the frame
如果改變0在iframe
元素不爲你工作,這些修復可能:
parent.document.getElementById(id).contentWindow.location.reload();
或
parent.frames["yourIframeName"].location.reload(); // requires iframe name attribute
或
parent.frames[0].location.reload(); // frames of immediate parent window
或
top.frames[0].location.reload(); // frames of top-most parent window
注意:如果使用name
方法要小心,不要使用公共價值爲name
,像「家」,例如,因爲它與所謂的home()
Firefox的功能衝突,因此Firefox將不會自動創建如果其名稱爲home
,則爲對iframe窗口的引用。最可靠的方法可能是使用window.frames[]
,因爲我相信已經存在了很長時間(適用於FF/Chrome/Safari/IE6 +(至少))
更深入(但非常小)例子如下,鉻,FF,以及IE測試:
文件#1:frametest.html(父窗口)
<!DOCTYPE html>
<html>
<body>
<iframe id="frame1Id" name="frame1Name" src="frame1.html"></iframe>
<iframe id="frame2Id" name="frame2Name" src="frame2.html"></iframe>
</body>
</html>
文件#2:frame1.html(幀1的src)
<!DOCTYPE html>
<html>
<body>
FRAME 1
<script>
document.body.style.backgroundColor="#ccc";
setTimeout(function(){document.body.style.backgroundColor="#fff";},100);
document.write(new Date());
</script>
<input type="button" onclick="parent.document.getElementById('frame2Id').src=parent.document.getElementById('frame2Id').src;" value="Refresh frame 2 by ID"/>
<input type="button" onclick="parent.frame2Name.location.reload();" value="Refresh frame 2 by Name"/>
</body>
</html>
文件#3:frame2.html(幀2的src)
<!DOCTYPE html>
<html>
<body>
FRAME 1
<script>
document.body.style.backgroundColor="#ccc";
setTimeout(function(){document.body.style.backgroundColor="#fff";},100);
document.write(new Date());
</script>
<input type="button" onclick="parent.document.getElementById('frame1Id').src=parent.document.getElementById('frame1Id').src;" value="Refresh frame 2 by ID"/>
<input type="button" onclick="parent.frame1Name.location.reload();" value="Refresh frame 2 by Name"/>
</body>
</html>
該實施例表明如何定義和通過id
和由name
操縱iframe
S,以及如何影響從一個不同iframe
內的一個iframe
。根據瀏覽器設置,原始策略可能適用,但您已經表示您的內容全部來自同一個域名,因此您應該在此處確定。
這兩個iframe是否從您的php腳本發送來自同一個域名?您不能更改其他域iframe。 – Skarlinski
是的。對不起,我忘了包括這一點。 –
如果在第一個iframe完成後手動運行refresh(「idString」)控制檯會發生什麼? – Skarlinski