javascript判定dom是否加载完毕
起主要明晰两个观念 1.window.onload:页面加载完毕,页面内全部组件(图片等)都可用。 2.dom 加载:指文档工具模子加载完毕,要先于window.onload变乱。 可以看出,当页面包括大量组件(出格是图片)的气象下,以上两种加载的时刻相隔将会很长,这时判定dom何时加载完成绩显得出格重要 页面的一些组件(css,image,flash)不会导致页面的DOM未构建完成。只有JS会阻塞页面的DOM节点的构建 function init() { // 假如该函数被挪用多次,直接返回 if (arguments.callee.done) return; // arguments.callee.done = true; // 破除对safari配置的按时器 if (_timer) clearInterval(_timer); alert(document.getElementById(“test”).id); }; // firefox和opera9.0 if (document.addEventListener) { document.addEventListener(“DOMContentLoaded”, init, false); } //ie document.write(“<script id=__ie_onload defer src=javascript:void(0)></script>”); var script = document.getElementById(“__ie_onload”); script.onreadystatechange = function() { if (this.readyState == “complete”) { init(); // call the onload handler } }; //Safari if (/WebKit/i.test(navigator.userAgent)) { // sniff var _timer = setInterval(function() { if (/loaded|complete/.test(document.readyState)) { init(); // call the onload handler } }, 10); } //其余赏识器直接用window.onload变乱 window.onload = init; 来历:http://www.wellpeter.com/?p=162 (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |