2009-06-23 52 views
0

這是一個跨瀏覽器的window.onload劇本,我想補充一個消息,當頁面加載<div id="message">Loading, please wait</div>)和它必須在頁面加載完消失。我怎樣才能做到這一點?請幫忙。信息上載

function init() { 
    if (arguments.callee.done) return; 
    arguments.callee.done = true; 
}; 

if (document.addEventListener) { 
    document.addEventListener("DOMContentLoaded", init, false); 
} 

/*@cc_on @*/ 
/*@if (@_win32) 
document.write("<script id=\"__ie_onload\" defer=\"defer\" src=\"javascript:void(0)\"><\/script>"); 
var script = document.getElementById("__ie_onload"); 
script.onreadystatechange = function() { 
    if (this.readyState == "complete") { 
    init(); 
    } 
}; 
/*@end @*/ 

if (/WebKit/i.test(navigator.userAgent)) { 
    var _timer = setInterval(function() { 
    if (/loaded|complete/.test(document.readyState)) { 
     clearInterval(_timer); 
     init(); 
    } 
    }, 10); 
} 

window.onload = init; 

回答

2

最簡單的方法是在DOM中的div隱藏的負載事件:

<html> 
    <body> 
     <div id="loading">The page is loading, please wait</div> 
     [...] 

     <script> 
      window.onload = function() { 
       document.getElementById('loading').style.display = "none"; 
      } 
     </script> 
    </body> 
</html> 

爲了確保這始終工作,甚至當用戶禁用JavaScript:

<html> 
    <body> 
     <script> 
      document.write('<div id="loading">The page is loading, please wait</div>'); 
     </script> 
     [...] 
     <script> 
      window.onload = function() { 
       document.getElementById('loading').style.display = "none"; 
      } 
     </script> 
    </body> 
</html> 
1

你正趕上該事件是一個觸發時DOM完成加載,在加載下一個步驟是body.onload(當所有的圖像和身體腳本加載)。

init(); // show the div 

window.onload = function(){ 
    // finished loading, hide the div 
} 
2

只需加載頁面,從開始就可以看到加載DIV。在init函數結束時隱藏它...?

+0

所以,不要在javascript中添加DIV,確保它是可見的,並在javascript中隱藏它時... – Ropstah 2009-06-23 12:02:29

+0

並且一定要在文檔中儘早加載,以便用戶不必等待爲它彈出。 – Sampson 2009-06-23 12:03:46