文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 35 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 387354 人次
  • RSS订阅: 文章|评论

小巧独立的Javascript库(2):DomReady.js

KY8 于 2009-03-01 16:14:02 发表JavaScript

订阅: http://www.kaiyuan8.org/Feed/Article_140.aspx
引用: 点这里获取地址 (UTF-8)
小巧独立的Javascript库(1):Events.js < 小巧独立的Javascript库(2):DomReady.js > 小巧独立的Javascript库(3):Sizzle JavaScript Selector Library

window.onload事件可以安全的执行javascript,因为该事件是在页面完全加载完后才开始执行(包括页面内的图片、flash等所有元素),不会因为JS需要对某个DOM 操作,而页面还没有加载该节点而引起错误。但是这种安全是需要付出代价的:如果某些图片(或者一些别的东西)加载特别慢,那么load事件会等到很久之后才会触发。针对这个问题,一些JS框架提供了一些补充方法。如:jquery的$(document).ready()、mootools的domready事件。都是在页面的DOM加载完毕后立即执行,而不需要等待漫长的图片下载过程。如果不使用这些框架,可以使用这个独立的DomReady.js

  1. (function(){ 
  2.  
  3.     var DomReady = window.DomReady = {}; 
  4.  
  5.     // Everything that has to do with properly supporting our document ready event. Brought over from the most awesome jQuery.  
  6.  
  7.     var userAgent = navigator.userAgent.toLowerCase(); 
  8.  
  9.     // Figure out what browser is being used 
  10.     var browser = { 
  11.         version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1], 
  12.         safari: /webkit/.test(userAgent), 
  13.         opera: /opera/.test(userAgent), 
  14.         msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )), 
  15.         mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent)) 
  16.     };     
  17.  
  18.     var readyBound = false;  
  19.     var isReady = false
  20.     var readyList = []; 
  21.  
  22.     // Handle when the DOM is ready 
  23.     function domReady() { 
  24.         // Make sure that the DOM is not already loaded 
  25.         if(!isReady) { 
  26.             // Remember that the DOM is ready 
  27.             isReady = true
  28.          
  29.             if(readyList) { 
  30.                 for(var fn = 0; fn < readyList.length; fn++) { 
  31.                     readyList[fn].call(window, []); 
  32.                 } 
  33.              
  34.                 readyList = []; 
  35.             } 
  36.         } 
  37.     }; 
  38.  
  39.     // From Simon Willison. A safe way to fire onload w/o screwing up everyone else. 
  40.     function addLoadEvent(func) { 
  41.       var oldonload = window.onload; 
  42.       if (typeof window.onload != 'function') { 
  43.         window.onload = func; 
  44.       } else { 
  45.         window.onload = function() { 
  46.           if (oldonload) { 
  47.             oldonload(); 
  48.           } 
  49.           func(); 
  50.         } 
  51.       } 
  52.     }; 
  53.  
  54.     // does the heavy work of working through the browsers idiosyncracies (let's call them that) to hook onload. 
  55.     function bindReady() { 
  56.         if(readyBound) { 
  57.             return
  58.         } 
  59.      
  60.         readyBound = true
  61.  
  62.         // Mozilla, Opera (see further below for it) and webkit nightlies currently support this event 
  63.         if (document.addEventListener && !browser.opera) { 
  64.             // Use the handy event callback 
  65.             document.addEventListener("DOMContentLoaded", domReady, false); 
  66.         } 
  67.  
  68.         // If IE is used and is not in a frame 
  69.         // Continually check to see if the document is ready 
  70.         if (browser.msie && window == top) (function(){ 
  71.             if (isReady) return
  72.             try { 
  73.                 // If IE is used, use the trick by Diego Perini 
  74.                 // http://javascript.nwbox.com/IEContentLoaded/ 
  75.                 document.documentElement.doScroll("left"); 
  76.             } catch(error) { 
  77.                 setTimeout(arguments.callee, 0); 
  78.                 return
  79.             } 
  80.             // and execute any waiting functions 
  81.             domReady(); 
  82.         })(); 
  83.  
  84.         if(browser.opera) { 
  85.             document.addEventListener( "DOMContentLoaded"function () { 
  86.                 if (isReady) return
  87.                 for (var i = 0; i < document.styleSheets.length; i++) 
  88.                     if (document.styleSheets[i].disabled) { 
  89.                         setTimeout( arguments.callee, 0 ); 
  90.                         return
  91.                     } 
  92.                 // and execute any waiting functions 
  93.                 domReady(); 
  94.             }, false); 
  95.         } 
  96.  
  97.         if(browser.safari) { 
  98.             var numStyles; 
  99.             (function(){ 
  100.                 if (isReady) return
  101.                 if (document.readyState != "loaded" && document.readyState != "complete") { 
  102.                     setTimeout( arguments.callee, 0 ); 
  103.                     return
  104.                 } 
  105.                 if (numStyles === undefined) { 
  106.                     var links = document.getElementsByTagName("link"); 
  107.                     for (var i=0; i < links.length; i++) { 
  108.                         if(links[i].getAttribute('rel') == 'stylesheet') { 
  109.                             numStyles++; 
  110.                         } 
  111.                     } 
  112.                     var styles = document.getElementsByTagName("style"); 
  113.                     numStyles += styles.length; 
  114.                 } 
  115.                 if (document.styleSheets.length != numStyles) { 
  116.                     setTimeout( arguments.callee, 0 ); 
  117.                     return
  118.                 } 
  119.              
  120.                 // and execute any waiting functions 
  121.                 domReady(); 
  122.             })(); 
  123.         } 
  124.  
  125.         // A fallback to window.onload, that will always work 
  126.         addLoadEvent(domReady); 
  127.     }; 
  128.  
  129.     // This is the public function that people can use to hook up ready. 
  130.     DomReady.ready = function(fn, args) { 
  131.         // Attach the listeners 
  132.         bindReady(); 
  133.      
  134.         // If the DOM is already ready 
  135.         if (isReady) { 
  136.             // Execute the function immediately 
  137.             fn.call(window, []); 
  138.         } else { 
  139.             // Add the function to the wait list 
  140.             readyList.push( function() { return fn.call(window, []); } ); 
  141.         } 
  142.     }; 
  143.      
  144.     bindReady(); 
  145.      
  146. })(); 

使用方法:

  1. <html lang="en"> 
  2. <head> 
  3.     <script src="domready.js" type="application/javascript"></script> 
  4.     <script type="application/javascript"> 
  5.         DomReady.ready(function() { 
  6.             alert('dom is ready'); 
  7.         }); 
  8.     </script> 
  9. </head> 
  10. <body> 
  11.  
  12. </body> 
  13. </html> 
被阅869次, 0投一票DomReady
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号