2015-08-13 51 views
0

當使用javascript動態生成iFrame時,當前在IE v9-11中遇到惱人的「白色閃光燈」問題。從本質上講,我試圖隱藏iframe,直到它被加載通過設置樣式顯示:無,然後將其設置爲顯示:一旦iframe已加載使用onload時顯示:null。這樣做 - 應該顯示iframe。看起來樣式沒有被移除,但函數正在執行。如何在加載後顯示iframe的想法值得讚賞。Java Script在加載iframe時隱藏白色閃光燈

的jsfiddle可以在這裏找到,看直播:http://jsfiddle.net/2zndpm8r/

var ifrm=document.createElement('IFRAME'); 
    ifrm.setAttribute('id','ifrm_ad'); 
    ifrm.setAttribute('height','90'); 
    ifrm.setAttribute('width','728'); 
    ifrm.setAttribute('frameborder','0'); 
    ifrm.setAttribute('scrolling','no'); 
    ifrm.setAttribute('marginwidth','0'); 
    ifrm.setAttribute('marginheight','0'); 
    ifrm.setAttribute('vspace','0'); 
    ifrm.setAttribute('hspace','0'); 
    ifrm.setAttribute('src','//placeholdit.imgix.net/~text?txtsize=23&txt=728%C3%9790&w=728&h=90') 
    ifrm.style.display='none'; 
    document.body.appendChild(ifrm); 
    document.getElementById('ifrm_ad').onload = function(){document.getElementById('ifrm_ad').style.display= null; console.log('show iframe');}; 
+0

所以iframe沒有被顯示? –

+0

在IE中,不,iframe未被加載。代碼在FF和Chrome中運行良好:/ –

+0

'似乎沒有刪除樣式'... Iframe沒有被加載 - - 兩個不同的東西 - 這是什麼? –

回答

1

一個建議

var ifrm=document.createElement('IFRAME'); 
    // ... 
    // snip - removed for brevity 
    // ... 
    ifrm.setAttribute('src','//placeholdit.imgix.net/~text?txtsize=23&txt=728%C3%9790&w=728&h=90') 
    ifrm.style.display='none'; 
    ifrm.onload = function(){ 
     ifrm.style.display= ''; // don't use null, use empty string 
     console.log('show iframe'); 
    }; 
    document.body.appendChild(ifrm); 

設置添加IFRM到DOM 也是之前的onload,你可以在這裏使用ifrm代替笨拙的getElementById

+0

轉折點,它完美的作品。謝謝。 –