JS DOMReady事件的六种实现方法总结(2)

(function(){ var domready = function(){ if (Browser.loaded) return; Browser.loaded = true; window.fireEvent('domready'); document.fireEvent('domready'); }; if (Browser.Engine.trident){ var temp = document.createElement_x('div'); (function(){ ($try(function(){ temp.doScroll('left'); return $(temp).inject(document.body).set('html', 'temp').dispose(); })) ? domready() : arguments.callee.delay(50); })(); } else if (Browser.Engine.webkit && Browser.Engine.version < 525){ (function(){ (['loaded', 'complete'].contains(document.readyState)) ? domready() : arguments.callee.delay(50); })(); } else { window.addEvent('load', domready); document.addEvent('DOMContentLoaded', domready); } })();

四、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: // 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(evtName, 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="https://:" +="" 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

// 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_x('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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzzggg.html