2013-10-25 95 views
3

我已經檢查了所有從另一個iframe帖子在stackoverflow重新加載iframe ...我嘗試了所有的解決方案,但它似乎並沒有幫助我!所以我的問題是,我有兩個iframe在同一頁面上。 iframe的源代碼是可以相互交互的php文件,但是我需要iframe來重新加載顯示更改的方式。我嘗試了很多不同的方式(我將在下面列出)。這些iframe來自同一個域。也許這是另一回事搞砸了嗎?提前致謝。從單獨的iframe重新加載iframe

不同的語句從一個iframe中稱爲:

parent.document.getElementById(id).src = parent.document.getElementById(id).src; 
parent.getElementById(id).location.reload(); 

試圖調用父窗口的功能父功能:

裏面的iframe -

parent.refresh(id); 

父窗口的工作函數 -

function refresh(id) { 
document.getElementById(id).src = document.getElementById(id).src; 
} 
+0

這兩個iframe是否從您的php腳本發送來自同一個域名?您不能更改其他域iframe。 – Skarlinski

+0

是的。對不起,我忘了包括這一點。 –

+0

如果在第一個iframe完成後手動運行refresh(「idString」)控制檯會發生什麼? – Skarlinski

回答

6

如果將name指定爲iframe,大多數瀏覽器將允許您通過name值訪問iframewindow對象。這與iframeid屬性有所不同,該屬性將爲您提供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),以確保你得到一個元素對象(而不是nullundefined) ,
  • 檢查parent.document.getElementById(id).tagName,以確保您使用的是正確的ID(tagName應該=== 「IFRAME」)

這條線:

parent.getElementById(id).location.reload(); 

有兩個問題:

  1. getElementById()document的函數,但它正在從parent其爲window對象調用,並且
  2. locationwindow對象的屬性。您正嘗試訪問iframe元素的location屬性,該屬性不存在。您需要參考iframewindow,而不是其元素。

除了name方法,還有其他方法來獲得iframe的窗口對象:

  1. document.getElementById('iframeId').contentWindow; // for supported browsers
  2. window.frames["iframeName"]; // assumes name property was set on the iframe
  3. 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。根據瀏覽器設置,原始策略可能適用,但您已經表示您的內容全部來自同一個域名,因此您應該在此處確定。

+0

這絕對是一個非常足智多謀的帖子!它幫助我解決了我的問題,並從中學到了很多東西。謝謝 :) –