信息上载
问题描述:
这是一个跨浏览器的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;
答
最简单的方法是在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>
答
你正赶上该事件是一个触发时DOM完成加载,在加载下一个步骤是body.onload(当所有的图像和身体脚本加载)。
init(); // show the div
window.onload = function(){
// finished loading, hide the div
}
答
只需加载页面,从开始就可以看到加载DIV。在init函数结束时隐藏它...?
所以,不要在javascript中添加DIV,确保它是可见的,并在javascript中隐藏它时... – Ropstah 2009-06-23 12:02:29
并且一定要在文档中尽早加载,以便用户不必等待为它弹出。 – Sampson 2009-06-23 12:03:46