domready.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * domready.js
  3. *
  4. * Cross browser mozilla's 'onDOMContentLoaded' implementation.
  5. * Executes a function when the dom tree is loaded without waiting for images.
  6. *
  7. * Based on +Element.Events.domready+ from Mootools open source project,
  8. * this tiny javascript library adds the emulated 'DOMContentLoaded' functionality.
  9. *
  10. * Features:
  11. * - No dependency on external libraries
  12. * - Compatible with Prototype.js
  13. *
  14. * Tested browsers (Windows):
  15. * - IE 7 (XP standalone)
  16. * - IE 6 SP2
  17. * - Firefox 2.0.0.4
  18. * - Opera 9.21
  19. *
  20. * Tested browsers (Mac OS X):
  21. * - Safari 2.0.4
  22. * - Firefox 2.0.0.4
  23. * - Mac Opera 9.21
  24. * - Mac IE 5.2.3
  25. *
  26. * Copyright (c) 2007 Takanori Ishikawa.
  27. * License: MIT-style license.
  28. *
  29. * MooTools Copyright:
  30. * copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
  31. *
  32. *
  33. * See Also:
  34. *
  35. * mootools
  36. * http://mootools.net/
  37. *
  38. * The window.onload Problem - Solved!
  39. * http://dean.edwards.name/weblog/2005/09/busted/
  40. *
  41. * [PATCH] Faster onload for Event.onload
  42. * http://dev.rubyonrails.org/ticket/5414
  43. * Changeset 6596: Support for "DOMContentLoaded" event handling (prototype.js event branch)
  44. * http://dev.rubyonrails.org/changeset/6596
  45. *
  46. */
  47. if (typeof Event == 'undefined') Event = new Object();
  48. /*
  49. * Registers function +fn+ will be executed when the dom
  50. * tree is loaded without waiting for images.
  51. *
  52. * Example:
  53. *
  54. * Event.domReady.add(function() {
  55. * ...
  56. * });
  57. *
  58. */
  59. Event.domReady = {
  60. add: function(fn) {
  61. //-----------------------------------------------------------
  62. // Already loaded?
  63. //-----------------------------------------------------------
  64. if (Event.domReady.loaded) return fn();
  65. //-----------------------------------------------------------
  66. // Observers
  67. //-----------------------------------------------------------
  68. var observers = Event.domReady.observers;
  69. if (!observers) observers = Event.domReady.observers = [];
  70. // Array#push is not supported by Mac IE 5
  71. observers[observers.length] = fn;
  72. //-----------------------------------------------------------
  73. // domReady function
  74. //-----------------------------------------------------------
  75. if (Event.domReady.callback) return;
  76. Event.domReady.callback = function() {
  77. if (Event.domReady.loaded) return;
  78. Event.domReady.loaded = true;
  79. if (Event.domReady.timer) {
  80. clearInterval(Event.domReady.timer);
  81. Event.domReady.timer = null;
  82. }
  83. var observers = Event.domReady.observers;
  84. for (var i = 0, length = observers.length; i < length; i++) {
  85. var fn = observers[i];
  86. observers[i] = null;
  87. fn(); // make 'this' as window
  88. }
  89. Event.domReady.callback = Event.domReady.observers = null;
  90. };
  91. //-----------------------------------------------------------
  92. // Emulates 'onDOMContentLoaded'
  93. //-----------------------------------------------------------
  94. var ie = !!(window.attachEvent && !window.opera);
  95. var webkit = navigator.userAgent.indexOf('AppleWebKit/') > -1;
  96. if (document.readyState && webkit) {
  97. // Apple WebKit (Safari, OmniWeb, ...)
  98. Event.domReady.timer = setInterval(function() {
  99. var state = document.readyState;
  100. if (state == 'loaded' || state == 'complete') {
  101. Event.domReady.callback();
  102. }
  103. }, 50);
  104. } else if (document.readyState && ie) {
  105. // Windows IE
  106. var src = (window.location.protocol == 'https:') ? '://0' : 'javascript:void(0)';
  107. document.write(
  108. '<script type="text/javascript" defer="defer" src="' + src + '" ' +
  109. 'onreadystatechange="if (this.readyState == \'complete\') Event.domReady.callback();"' +
  110. '><\/script>');
  111. } else {
  112. if (window.addEventListener) {
  113. // for Mozilla browsers, Opera 9
  114. document.addEventListener("DOMContentLoaded", Event.domReady.callback, false);
  115. // Fail safe
  116. window.addEventListener("load", Event.domReady.callback, false);
  117. } else if (window.attachEvent) {
  118. window.attachEvent('onload', Event.domReady.callback);
  119. } else {
  120. // Legacy browsers (e.g. Mac IE 5)
  121. var fn = window.onload;
  122. window.onload = function() {
  123. Event.domReady.callback();
  124. if (fn) fn();
  125. }
  126. }
  127. }
  128. }
  129. }