2011-11-28 54 views
2

普通JS - 請不要jQuery建議 - 這是一個書籤,它需要儘可能使用普通的JS。在IE中正確訪問動態iFrame

我希望有人知道答案,因爲我無法可靠地創建一個小提琴。

此代碼將在其插入的頁面範圍內運行 - 它在Fx6-9,safari和Windows XP和OSX上的最新Chromes中完美工作 - 當我嘗試訪問iFrame時,只有IE瀏覽器給出了未定義的內容

var zContainer = document.getElementById('zContainer'); 
if (zContainer==null) { 
    zContainer = document.createElement("div"); 
    zContainer.id="zContainer"; 
    document.body.appendChild(zContainer); 
}  

var zStuff = {}; // minimise window var footprint 
zStuff.html = '<body>Hello</body>'; 
if (!zFrame) { // did we already have one? 
    zFrame = document.createElement("iframe"); 
    zFrame.id="zIframeId"; zFrame.name="zIframeName"; zFrame.frameBorder="0"; 
    zIFS = zFrame.style; zIFS.border="0"; zIFS.width="500px"; zIFS.height="500px"; zIFS.backgroundColor="white"; zIFS.display="block"; 
    zContainer.appendChild(zFrame); // append to div 

    zFrame = window.frames["zIframeName"]; // undefined in IE8 !!!!! 
    // zFrame = document.getElementById("zIframeId"); // undefined in IE8 !!!!! 

    zFrame.src="javascript:'<body></body>'"; // initialise body 
    zFrame.document.write(zStuff.html); // or zFrame.contentDocument.write 
    zFrame.document.close(); 
    // zFrame.document.body.innerHTML=zStuff.html; // also does not work 

// zFrame.src="javascript:'"+zStuff.html+"'"; // alternative method - either one works in Fx/Chrome 
} 

感謝您的任何提示和不投票。我希望多所社區將作爲 對我很有幫助,因爲我已經把它在過去的一年半...


更新 - 因爲我張貼的代碼有一些殘餘無奈之下,我把它改成

if (!zFrame) { // did we already have one? 
    zFrame = document.createElement("iframe"); 
    zFrame.id="zIframeId"; zFrame.name="zIframeName"; zFrame.frameBorder="0"; 
    zIFS = zFrame.style; zIFS.border="0"; zIFS.width="500px"; zIFS.height="500px"; zIFS.backgroundColor="white"; zIFS.display="block"; 
    zContainer.appendChild(zFrame); // append to div 

    zFrame.src="javascript:'<body></body>'"; // initialise body 

    zFrame.document.write(zStuff.html); // or zFrame.contentDocument.write 
    zFrame.document.close(); 
} 

上述取代現在我與在zStuff.html 的代碼,而不是隻更換的iFrame內容的頁面 - 這也打破了外匯

現在,我必須這樣做外匯其中IE也並不介意,但還是替換窗口,而不是iFrame的

if (!zFrame) { 
    zFrame = document.createElement("iframe"); 
    zFrame.scrolling="no"; zFrame.id="zIframeId"; zFrame.name="zIframeName"; zFrame.frameBorder="0"; 
    zIFS = zFrame.style; zIFS.border="0px none"; zIFS.width="549px"; zIFS.height="510px"; zIFS.backgroundColor="white"; zIFS.display="block"; 
    zDRContainer.appendChild(zFrame); 
    zFrame.src="javascript:'<body></body>'"; 
    setTimeout(function() { 
    var zFrame = window.frames["zIframeName"]; // this is needed for the document.write 
    zFrame.document.write(zStuff.html); 
    zFrame.document.close(); 
    },100); 
} 
+1

那麼它沒有任何意義..你已經有'zFrame'引用iframe。什麼可以用'zFrame = window.frames [「zIframeName」];'來實現,即使它工作? – Esailija

+0

有一點zContainer是未定義的onload,因爲'zContainer' dosn't尚存在 – david

+0

至少爲變量'zFrame'設置變量 – david

回答

3

我在聊天中提出的黑客:

var a = setInterval(function(){ 

    try{ 
    zFrame.contentWindow.document.write(zStuff.html); 
    zFrame.contentWindow.document.close(); 
    clearInterval(a); 
    } 
    catch(e){} 

}, 10); 

由於它不是IE允許訪問contentWindow屬性時知道,這將繼續嘗試,直到它被允許。

+0

我會測試這個,並找回 – mplungjan

+0

似乎工程! – mplungjan

+0

嗯 - 應該怎麼工作是zframe.onload .......我會嘗試 – mplungjan