1、什么时候触发这两个事件?javascript
一、当 onload
事件触发时,页面上全部的DOM,样式表,脚本,图片,flash都已经加载完成了。css
二、当 DOMContentLoaded
事件触发时,仅当DOM加载完成,不包括样式表,图片,flash。html
2、为何要区分?前端
开发中咱们常常须要给一些元素的事件绑定处理函数。但问题是,若是那个元素尚未加载到页面上,可是绑定事件已经执行完了,是没有效果的。这两个事件大体就是用来避免这样一种状况,将绑定的函数放在这两个事件的回调中,保证能在页面的某些元素加载完毕以后再绑定事件的函数。java
固然DOMContentLoaded机制更加合理,由于咱们能够容忍图片,flash延迟加载,却不能够容忍看见内容后页面不可交互。jquery
这里又要牵扯到页面加载渲染的原理了:web
一、加载样式表会阻塞外链脚本的执行promise
一些Gecko和Webkit引擎版本的浏览器,包括IE8在内,会同时发起多个Http请求来并行下在样式表和脚本。但脚本不会被执行,直到样式被加载完成。在未加载完以前甚至页面也不会被渲染。可是在opera中样式的加载不会阻塞脚本的执行。浏览器
所以:目前通用的做法是把脚本和样式都之外链形式引入,甚至在jquery的官方文档中也是这样推荐的。对于大部分脚原本说,这样的脚本等待外链的机制仍是有意义的,好比一些DOM和样式操做须要读取元素的位置,颜色等。这就须要样式先于脚本加载前端框架
检验方法:尝试强制使服务器端使style延迟一段时间才加载(甚至10秒),测试的结果是,在某些版本的Firefox,Chrome中最后一段脚本仍然是能够读出style的属性值(由于style始终先于javascript加载),好比#FF0000或者rgb(255, 0, 0),而这验证了我上面的说法。而在opera中却没法读出style的属性。代码以下:
html 文件内容 <!DOCTYPE html> <head> <linkrel="stylesheet"href="stylesheet.css"> <scriptsrc="script.js"></script> </head> <body> <divid="element">The element</div>< /body> </html> stylesheet.css 文件内容 #element { color: red; } script.js文件内容 document.addEventListener('DOMContentLoaded',function(){ alert(getComputedStyle(document.getElementById('element'),null).color);}, false);
二、各大javascript框架如何实现domReady事件的
早期版本的浏览器是没有DOMContentLoaded事件的那么它们怎么模拟实现相似功能呢?先来讲说原理
(1)、若是是webkit引擎则轮询document的readyState属性,当值为loaded或者complete时则触发DOMContentLoaded事件,对webkit525以后版本直接能够注册DOMContentLoaded事件
if(Browser.Engine.webkit){ timer = window.setInterval(function(){ if(/loaded|complete/.test(document.readyState)) fireContentLoadedEvent(); },0); }
(2)、IE处理方式有多种
a、在页面临时插入一个script元素,并设置defer属性,最后把该脚本加载完成视做DOMContentLoaded事件来触发。这样作有一个问题是,若是插入脚本的页面包含iframe的话,会等到iframe加载完才触发,其实这与onload是无异的。即这个方法不许确。
b、经过setTiemout来不断的调用documentElement的doScroll方法,直到调用成功则出触发DOMContentLoaded。这样作的原理是在IE下,DOM的某些方法只有在DOM解析完成后才能够调用,doScroll就是这样一个方法,反过来当能调用doScroll的时候便是DOM解析完成之时,与prototype中的document.write相比,该方案能够解决页面有iframe时失效的问题
c、首先注册document的onreadystatechange事件,但经测试后该方法与window.onload至关,效果不大。下面是jquery作的兼容性处理代码。
document.attachEvent("onreadystatechange", function(){ if( document.readyState ==="complete"){ document.detachEvent("onreadystatechange", arguments.callee ); jQuery.ready();} });
接下来具体看一看几大前端框架是如何综合运用这几个方法的。
jQuery.ready.promise = function( obj ) {//定义一个状态机 if ( !readyList ) {//保证页面只建立一个延迟对象,屡次使用$.ready() 则直接使用延迟对象done方法加入回调队列 readyList = jQuery.Deferred();//异步延迟对象 // readyRE = /complete|loaded|interactive/, // Catch cases where $(document).ready() is called after the browser event has already occurred. // we once tried to use readyState "interactive" here, but it caused issues like the one // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 if ( document.readyState === "complete" ) {//
这个属性是只读的,传回值有如下的可能:
//0-UNINITIALIZED:XML 对象被产生,但没有任何文件被加载。
//1-LOADING:加载程序进行中,但文件还没有开始解析。
//2-LOADED:部分的文件已经加载且进行解析,但对象模型还没有生效。
//3-INTERACTIVE:仅对已加载的部分文件有效,在此状况下,对象模型是有效但只读的。
//4-COMPLETED:文件已彻底加载,表明加载成功
// Handle it asynchronously to allow scripts the opportunity to delay ready setTimeout( jQuery.ready ); // Standards-based browsers support DOMContentLoaded } else if ( document.addEventListener ) {//符合W3C标准的浏览器 // Use the handy event callback document.addEventListener( "DOMContentLoaded", completed, false ); // A fallback to window.onload, that will always work window.addEventListener( "load", completed, false );//仍是给load事件注册了事件,以防不测,作为回滚用 // If IE event model is used } else { // Ensure firing before onload, maybe late but safe also for iframes document.attachEvent( "onreadystatechange", completed ); // A fallback to window.onload, that will always work window.attachEvent( "onload", completed ); // If IE and not a frame // continually check to see if the document is ready var top = false; try {//判断是否为iframe,若是不是的话采用不断的轮询scorll的方法 top = window.frameElement == null && document.documentElement; } catch(e) {} if ( top && top.doScroll ) { (function doScrollCheck() { if ( !jQuery.isReady ) { try { // Use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ top.doScroll("left"); } catch(e) { return setTimeout( doScrollCheck, 50 ); } // detach all dom ready events detach(); // and execute any waiting functions jQuery.ready();//实际:readyList.resolveWith( document, [ jQuery ] );
} })(); } } } return readyList.promise( obj ); };
再贴上几段其余框架的代码,大同小异,就不具体分析了
prototype
(function(GLOBAL) { /* Support for the DOMContentLoaded event is based on work by Dan Webb, Matthias Miller, Dean Edwards, John Resig, and Diego Perini. */ var TIMER; function fireContentLoadedEvent() { if (document.loaded) return; if (TIMER) window.clearTimeout(TIMER); document.loaded = true; document.fire('dom:loaded'); } function checkReadyState() { if (document.readyState === 'complete') { document.detachEvent('onreadystatechange', checkReadyState); fireContentLoadedEvent(); } } function pollDoScroll() { try { document.documentElement.doScroll('left'); } catch (e) { TIMER = pollDoScroll.defer(); return; } fireContentLoadedEvent(); } if (document.readyState === 'complete') { // We must have been loaded asynchronously, because the DOMContentLoaded // event has already fired. We can just fire `dom:loaded` and be done // with it. fireContentLoadedEvent(); return; } if (document.addEventListener) { // All browsers that support DOM L2 Events support DOMContentLoaded, // including IE 9. document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); } else { document.attachEvent('onreadystatechange', checkReadyState); if (window == top) TIMER = pollDoScroll.defer(); } // Worst-case fallback. Event.observe(window, 'load', fireContentLoadedEvent); })(this);
mootools
(function(GLOBAL) { /* Support for the DOMContentLoaded event is based on work by Dan Webb, Matthias Miller, Dean Edwards, John Resig, and Diego Perini. */ var TIMER; function fireContentLoadedEvent() { if (document.loaded) return; if (TIMER) window.clearTimeout(TIMER); document.loaded = true; document.fire('dom:loaded'); } function checkReadyState() { if (document.readyState === 'complete') { document.detachEvent('onreadystatechange', checkReadyState); fireContentLoadedEvent(); } } function pollDoScroll() { try { document.documentElement.doScroll('left'); } catch (e) { TIMER = pollDoScroll.defer(); return; } fireContentLoadedEvent(); } if (document.readyState === 'complete') { // We must have been loaded asynchronously, because the DOMContentLoaded // event has already fired. We can just fire `dom:loaded` and be done // with it. fireContentLoadedEvent(); return; } if (document.addEventListener) { // All browsers that support DOM L2 Events support DOMContentLoaded, // including IE 9. document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); } else { document.attachEvent('onreadystatechange', checkReadyState); if (window == top) TIMER = pollDoScroll.defer(); } // Worst-case fallback. Event.observe(window, 'load', fireContentLoadedEvent); })(this);
纸上学来终觉浅,绝知此事要躬行。本身写一段。
(function(window,undefined){ hobo = {} var readyList = [], _isReady =false; function readyFn(){ console.log(event.type) if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) { detach(); _isReady =true; fireReady();
} }
function fireReady(){
for (var i = 0,fn; fn = readyList[i++];) { fn(); };
readyList = null;
fireReady = function(){}//惰性函数,防止IE9二次调用
}
function detach() { if ( document.addEventListener ) { document.removeEventListener( "DOMContentLoaded", readyFn, false ); window.removeEventListener( "load", readyFn, false ); } else { document.detachEvent( "onreadystatechange", readyFn ); window.detachEvent( "onload", readyFn ); } } hobo.ready = function(fn){ if(readyList){ readyList.push(fn) } if(readyList.length>1){ return; } if(document.readyState === 'complete'){ setTimeout(readyFn); }else if (document.addEventListener) {//符合W3C 则监听 DOMContentLoaded和load事件 console.log('addEventListener') document.addEventListener('DOMContentLoaded',readyFn,false); document.addEventListener('DOMContentLoaded',readyFn,false); }else{//针对IE console.log('attachEvent') document.attachEvent('onreadystatechange',readyFn); document.attachEvent('onload',readyFn); } //针对IE而且非frame var top = false; try{ top = window.frameElement===null&&document.documentElement }catch(e){} if(top&&top.doScroll){ (function doScrollCheck(){ if (!_isReady) { try {//每隔50秒轮询 检测是否支持doScroll()方法 top.doScroll("left"); } catch(e) { return setTimeout( doScrollCheck, 50 ); } }; }) } } window.hobo =hobo }(window,void 0)) //使用 hobo.ready(function(){ console.log(11111); }) hobo.ready(function(){ console.log(22222); })