原来比较常用的是window的onload 事件,而该事件的实际效果是:当页面解析/DOM树建立完成,并完成了诸如图片、脚本、样式表甚至是iframe中所有资源的下载后才触发的。这对于很多 实际的应用而言有点太“迟”了,比较影响用户体验。为了解决这个问题,ff中便增加了一个DOMContentLoaded方法,与onload相比,该 方法触发的时间更早,它是在页面的DOM内容加载完成后即触发,而无需等待其他资源的加载。Webkit引擎从版本525(Webkit nightly 1/2008:525+)开始也引入了该事件,Opera中也包含该方法。到目前为止主流的IE仍然没有要添加的意思。虽然IE下没有,但总是有解决办法 的,下文对比了一下几大主流框架对于该事件的兼容性版本实现方案,涉及的框架包括: Prototype jQeury moontools dojo yui ext 一、Prototype 实现代码 复制代码 代码如下: (function() { /* Support for the DOMContentLoaded event is based on work by Dan Webb, Matthias Miller, Dean Edwards and John Resig. */ var timer; function fireContentLoadedEvent() { if (document.loaded) return; if (timer) window.clearInterval(timer); document.fire("dom:loaded"); document.loaded = true; } if (document.addEventListener) { if (Prototype.Browser.WebKit) { timer = window.setInterval(function() { if (/loaded|complete/.test(document.readyState)) fireContentLoadedEvent(); }, 0); Event.observe(window, "load", fireContentLoadedEvent); } else { document.addEventListener("DOMContentLoaded", fireContentLoadedEvent, false); } } else { document.write("<"+"script id=__onDOMContentLoaded defer src=//:></script>"); $("__onDOMContentLoaded").onreadystatechange = function() { if (this.readyState == "complete") { this.onreadystatechange = null; fireContentLoadedEvent(); } }; } })();
实现思路如下: 如果是webkit则轮询document的readyState属性,如果该属性的值为loaded或complete则触发DOMContentLoaded事件,为保险起见,将该事件注册到window.onload上。 如果是FF则直接注册DOMContentLoaded事件。 如果是IE则使用document.write往页面中加入一个script元素,并设置defer属性,最后是把该脚本的加载完成视作DOMContentLoaded事件来触发。 该实现方式的问题主要有两点:第一、通过document.write写script并设置defer的方法在页面包含iframe的情况下,会等到 iframe内的内容加载完后才触发,这与onload没有太大的区别;第二、Webkit在525以上的版本引入了DOMContentLoaded方 法,因此在这些版本中无需再通过轮询来实现,可以优化。 二、jQuery 复制代码 代码如下: function bindReady(){ if ( readyBound ) return; readyBound = true; // Mozilla, Opera and webkit nightlies currently support this event if ( document.addEventListener ) { // Use the handy event callback document.addEventListener( "DOMContentLoaded", function(){ document.removeEventListener( "DOMContentLoaded", arguments.callee, false ); jQuery.ready(); }, false ); // If IE event model is used } else if ( document.attachEvent ) { // ensure firing before onload, // maybe late but safe also for iframes document.attachEvent("onreadystatechange", function(){ if ( document.readyState === "complete" ) { document.detachEvent( "onreadystatechange", arguments.callee ); jQuery.ready(); } }); // If IE and not an iframe // continually check to see if the document is ready if ( document.documentElement.doScroll && typeof window.frameElement === "undefined" ) (function(){ if ( jQuery.isReady ) return; try { // If IE is used, use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ document.documentElement.doScroll("left"); } catch( error ) { setTimeout( arguments.callee, 0 ); return; } // and execute any waiting functions jQuery.ready(); })(); } // A fallback to window.onload, that will always work jQuery.event.add( window, "load", jQuery.ready ); }
实现思路如下: 如果是IE则使用doScroll方法来实现。 如果是小于525版本的Webkit则通过轮询document.readyState来实现。 其他的(FF/Webkit高版/Opera)则直接注册DOMContentLoaded事件 Moontools的实现方案prototype和jQeury中的综合体,对webkit做了版本判断则使得该方案更加的健壮。在doScroll的实现方面,与jQuery相比,这里是新建了一个div元素,并且在使用完毕后进行销毁,而jQuery则直接使用了documentElement的 doScroll来检测,更简单高效一些。 四、Dojo 复制代码 代码如下: // START DOMContentLoaded // Mozilla and Opera 9 expose the event we could use if(document.addEventListener){ // NOTE: // due to a threading issue in Firefox 2.0, we can"t enable // DOMContentLoaded on that platform. For more information, see: // http://trac.dojotoolkit.org/ticket/1704 if(dojo.isOpera || dojo.isFF >= 3 || (dojo.isMoz && dojo.config.enableMozDomContentLoaded === true)){ document.addEventListener("DOMContentLoaded", dojo._loadInit, null); } // mainly for Opera 8.5, won"t be fired if DOMContentLoaded fired already. // also used for Mozilla because of trac #1640 window.addEventListener("load", dojo._loadInit, null); } if(dojo.isAIR){ window.addEventListener("load", dojo._loadInit, null); }else if(/(WebKit|khtml)/i.test(navigator.userAgent)){ // sniff dojo._khtmlTimer = setInterval(function(){ if(/loaded|complete/.test(document.readyState)){ dojo._loadInit(); // call the onload handler } }, 10); } // END DOMContentLoaded (function(){ var _w = window; var _handleNodeEvent = function(/*String*/evtName, /*Function*/fp){ // summary: // non-destructively adds the specified function to the node"s // evtName handler. // evtName: should be in the form "onclick" for "onclick" handlers. // Make sure you pass in the "on" part. var oldHandler = _w[evtName] || function(){}; _w[evtName] = function(){ fp.apply(_w, arguments); oldHandler.apply(_w, arguments); }; }; if(dojo.isIE){ // for Internet Explorer. readyState will not be achieved on init // call, but dojo doesn"t need it however, we"ll include it // because we don"t know if there are other functions added that // might. Note that this has changed because the build process // strips all comments -- including conditional ones. if(!dojo.config.afterOnLoad){ document.write("<scr"+"ipt defer="" src="//:" +="" onreadystatechange="if(this.readyState=="complete"){" + dojo._scopeName + "._loadInit();}">" + "</scr"+"ipt>" ); } try{ document.namespaces.add("v","urn:schemas-microsoft-com:vml"); document.createStyleSheet().addRule("v\:*", "behavior:url(#default#VML)"); }catch(e){} } // FIXME: dojo.unloaded requires dojo scope, so using anon function wrapper. _handleNodeEvent("onbeforeunload", function() { dojo.unloaded(); }); _handleNodeEvent("onunload", function() { dojo.windowUnloaded(); }); })();
实现思路如下: 如果是Opera或FF3以上版本则直接注册DOMContentLoaded<事件,为保险起见,同时也注册了window.onload事件。 对于webkit则通过轮询document.readyState来实现。 如果是Air则只注册widnow.onload事件。 如果是IE则通过往页面写带defer属性的script并注册其onreadystatechange事件来实现。 Dojo在IE下的实现方案同样无法解决iframe的问题,而由于在FF2 下会有一个非常奇怪的Bug,因此默认只在FF3以上版本上使用DOMContentLoaded事件,同时又给了一个配置 -dojo.config.enableMozDomContentLoaded,如果在FF下将该配置设置为true则依然会使用 DOMContentLoaded来实现,这一点充分考虑到了灵活性。对于webkit的实现,与prototype一样有优化的空间。 五、YUI 复制代码 代码如下: (function() { /*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller */ // Internet Explorer: use the readyState of a defered script. // This isolates what appears to be a safe moment to manipulate // the DOM prior to when the document"s readyState suggests // it is safe to do so. if (EU.isIE) { // Process onAvailable/onContentReady items when the // DOM is ready. YAHOO.util.Event.onDOMReady( YAHOO.util.Event._tryPreloadAttach, YAHOO.util.Event, true); var n = document.createElement("p"); EU._dri = setInterval(function() { try { // throws an error if doc is not ready n.doScroll("left"); clearInterval(EU._dri); EU._dri = null; EU._ready(); n = null; } catch (ex) { } }, EU.POLL_INTERVAL); // The document"s readyState in Safari currently will // change to loaded/complete before images are loaded. } else if (EU.webkit && EU.webkit < 525) { EU._dri = setInterval(function() { var rs=document.readyState; if ("loaded" == rs || "complete" == rs) { clearInterval(EU._dri); EU._dri = null; EU._ready(); } }, EU.POLL_INTERVAL); // FireFox and Opera: These browsers provide a event for this // moment. The latest WebKit releases now support this event. } else { EU._simpleAdd(document, "DOMContentLoaded", EU._ready); } ///////////////////////////////////////////////////////////// EU._simpleAdd(window, "load", EU._load); EU._simpleAdd(window, "unload", EU._unload); EU._tryPreloadAttach(); })();
实现思路与Moontools一样 六、EXT 复制代码 代码如下: function initDocReady(){ var COMPLETE = "complete"; docReadyEvent = new Ext.util.Event(); if (Ext.isGecko || Ext.isOpera) { DOC.addEventListener(DOMCONTENTLOADED, fireDocReady, false); } else if (Ext.isIE){ DOC.write("<script id=" + IEDEFERED + " defer=defer src="/%27+%27/:"></script>"); DOC.getElementById(IEDEFERED).onreadystatechange = function(){ if(this.readyState == COMPLETE){ fireDocReady(); } }; } else if (Ext.isWebKit){ docReadyProcId = setInterval(function(){ if(DOC.readyState == COMPLETE) { fireDocReady(); } }, 10); } // no matter what, make sure it fires on load E.on(WINDOW, "load", fireDocReady); }