ondomready.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*!
  2. * onDomReady.js 1.4.0 (c) 2013 Tubal Martin - MIT license
  3. *
  4. * Specially modified to work with Holder.js
  5. */
  6. ;(function(name, global, callback){
  7. global[name] = callback;
  8. })("onDomReady", this,
  9. (function(win) {
  10. 'use strict';
  11. //Lazy loading fix for Firefox < 3.6
  12. //http://webreflection.blogspot.com/2009/11/195-chars-to-help-lazy-loading.html
  13. if (document.readyState == null && document.addEventListener) {
  14. document.addEventListener("DOMContentLoaded", function DOMContentLoaded() {
  15. document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false);
  16. document.readyState = "complete";
  17. }, false);
  18. document.readyState = "loading";
  19. }
  20. var doc = win.document,
  21. docElem = doc.documentElement,
  22. LOAD = "load",
  23. FALSE = false,
  24. ONLOAD = "on"+LOAD,
  25. COMPLETE = "complete",
  26. READYSTATE = "readyState",
  27. ATTACHEVENT = "attachEvent",
  28. DETACHEVENT = "detachEvent",
  29. ADDEVENTLISTENER = "addEventListener",
  30. DOMCONTENTLOADED = "DOMContentLoaded",
  31. ONREADYSTATECHANGE = "onreadystatechange",
  32. REMOVEEVENTLISTENER = "removeEventListener",
  33. // W3C Event model
  34. w3c = ADDEVENTLISTENER in doc,
  35. top = FALSE,
  36. // isReady: Is the DOM ready to be used? Set to true once it occurs.
  37. isReady = FALSE,
  38. // Callbacks pending execution until DOM is ready
  39. callbacks = [];
  40. // Handle when the DOM is ready
  41. function ready( fn ) {
  42. if ( !isReady ) {
  43. // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
  44. if ( !doc.body ) {
  45. return defer( ready );
  46. }
  47. // Remember that the DOM is ready
  48. isReady = true;
  49. // Execute all callbacks
  50. while ( fn = callbacks.shift() ) {
  51. defer( fn );
  52. }
  53. }
  54. }
  55. // The ready event handler
  56. function completed( event ) {
  57. // readyState === "complete" is good enough for us to call the dom ready in oldIE
  58. if ( w3c || event.type === LOAD || doc[READYSTATE] === COMPLETE ) {
  59. detach();
  60. ready();
  61. }
  62. }
  63. // Clean-up method for dom ready events
  64. function detach() {
  65. if ( w3c ) {
  66. doc[REMOVEEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE );
  67. win[REMOVEEVENTLISTENER]( LOAD, completed, FALSE );
  68. } else {
  69. doc[DETACHEVENT]( ONREADYSTATECHANGE, completed );
  70. win[DETACHEVENT]( ONLOAD, completed );
  71. }
  72. }
  73. // Defers a function, scheduling it to run after the current call stack has cleared.
  74. function defer( fn, wait ) {
  75. // Allow 0 to be passed
  76. setTimeout( fn, +wait >= 0 ? wait : 1 );
  77. }
  78. // Attach the listeners:
  79. // Catch cases where onDomReady is called after the browser event has already occurred.
  80. // we once tried to use readyState "interactive" here, but it caused issues like the one
  81. // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
  82. if ( doc[READYSTATE] === COMPLETE ) {
  83. // Handle it asynchronously to allow scripts the opportunity to delay ready
  84. defer( ready );
  85. // Standards-based browsers support DOMContentLoaded
  86. } else if ( w3c ) {
  87. // Use the handy event callback
  88. doc[ADDEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE );
  89. // A fallback to window.onload, that will always work
  90. win[ADDEVENTLISTENER]( LOAD, completed, FALSE );
  91. // If IE event model is used
  92. } else {
  93. // Ensure firing before onload, maybe late but safe also for iframes
  94. doc[ATTACHEVENT]( ONREADYSTATECHANGE, completed );
  95. // A fallback to window.onload, that will always work
  96. win[ATTACHEVENT]( ONLOAD, completed );
  97. // If IE and not a frame
  98. // continually check to see if the document is ready
  99. try {
  100. top = win.frameElement == null && docElem;
  101. } catch(e) {}
  102. if ( top && top.doScroll ) {
  103. (function doScrollCheck() {
  104. if ( !isReady ) {
  105. try {
  106. // Use the trick by Diego Perini
  107. // http://javascript.nwbox.com/IEContentLoaded/
  108. top.doScroll("left");
  109. } catch(e) {
  110. return defer( doScrollCheck, 50 );
  111. }
  112. // detach all dom ready events
  113. detach();
  114. // and execute any waiting functions
  115. ready();
  116. }
  117. })();
  118. }
  119. }
  120. function onDomReady( fn ) {
  121. // If DOM is ready, execute the function (async), otherwise wait
  122. isReady ? defer( fn ) : callbacks.push( fn );
  123. }
  124. // Add version
  125. onDomReady.version = "1.4.0";
  126. // Add method to check if DOM is ready
  127. onDomReady.isReady = function(){
  128. return isReady;
  129. };
  130. return onDomReady;
  131. })(this));