Posted By

freelancephp on 03/01/10


Tagged

event onload domready DOMContentLoaded IEContentLoaded


Versions (?)

domReady v3


 / Published in: JavaScript
 

URL: http://www.freelancephp.net/domready-javascript-object-cross-browser/

Execute functions when the DOM is ready (cross-browser compatibility).

  1. /**
  2.  * domReady
  3.  *
  4.  * @fileOverview
  5.  * Cross browser object to attach functions that will be called
  6.  * immediatly when the DOM is ready.
  7.  * Released under MIT license.
  8.  * @version 3.0.0
  9.  * @author Victor Villaverde Laan
  10.  * @license MIT
  11.  * @link http://www.freelancephp.net/domready-javascript-object-cross-browser/
  12.  * @link https://github.com/freelancephp/DOMReady
  13.  */
  14. (function (window) {
  15.  
  16. 'use strict';
  17.  
  18. var document = window.document;
  19. var fns = [];
  20. var args = [];
  21. var isReady = false;
  22. var errorHandler = null;
  23.  
  24. /**
  25.   * Call a ready handler
  26.   * @private
  27.   * @param {function} fn
  28.   */
  29. var call = function (fn) {
  30. try {
  31. // call function
  32. fn.apply(this, args);
  33. } catch (e) {
  34. // error occured while executing function
  35. if (errorHandler !== null) {
  36. errorHandler.call(this, e);
  37. }
  38. }
  39. };
  40.  
  41. /**
  42.   * Call all ready handlers
  43.   * @private
  44.   */
  45. var run = function () {
  46. var x;
  47.  
  48. isReady = true;
  49.  
  50. // call all registered functions
  51. for (x = 0; x < fns.length; x = x + 1) {
  52. call(fns[x]);
  53. }
  54.  
  55. // clear handlers
  56. fns = [];
  57. };
  58.  
  59. /**
  60.   * Initialize
  61.   * @private
  62.   */
  63. var init = function () {
  64. if (window.addEventListener) {
  65. // for all browsers except IE
  66. document.addEventListener('DOMContentLoaded', function () { run(); }, false);
  67. } else {
  68. // for IE
  69. // code taken from http://javascript.nwbox.com/IEContentLoaded/
  70. var poll = function () {
  71. // check IE's proprietary DOM members
  72. if (!document.uniqueID && document.expando) {
  73. return;
  74. }
  75.  
  76. // you can create any tagName, even customTag like <document :ready />
  77. var tempNode = document.createElement('document:ready');
  78.  
  79. try {
  80. // see if it throws errors until after ondocumentready
  81. tempNode.doScroll('left');
  82.  
  83. // call run
  84. run();
  85. } catch (e) {
  86. window.setTimeout(poll, 10);
  87. }
  88. };
  89.  
  90. // trying to always fire before onload
  91. document.onreadystatechange = function() {
  92. if (document.readyState === 'complete') {
  93. document.onreadystatechange = null;
  94. run();
  95. }
  96. };
  97.  
  98. poll();
  99. }
  100. };
  101.  
  102. /**
  103.   * @namespace domReady
  104.   *
  105.   * @public
  106.   * @param {function} fn
  107.   * @return {domReady}
  108.   */
  109. var domReady = function (fn) {
  110. return domReady.on(fn);
  111. };
  112.  
  113. /**
  114.   * Add code or function to execute when the DOM is ready
  115.   * @public
  116.   * @param {function} fn
  117.   * @return {domReady}
  118.   */
  119. domReady.on = function (fn) {
  120. // call imediately when DOM is already ready
  121. if (isReady) {
  122. call(fn);
  123. } else {
  124. // add to the list
  125. fns[fns.length] = fn;
  126. }
  127.  
  128. return this;
  129. };
  130.  
  131. /**
  132.   * Set params that will be passed to every ready handler
  133.   * @public
  134.   * @param {Array.<*>} params
  135.   * @return {domReady}
  136.   */
  137. domReady.params = function (params) {
  138. args = params;
  139. return this;
  140. };
  141.  
  142. /**
  143.   * Set error callback
  144.   * @public
  145.   * @param {function([Error|string])} fn
  146.   * @return {domReady}
  147.   */
  148. domReady.error = function (fn) {
  149. errorHandler = fn;
  150. return this;
  151. };
  152.  
  153. // initialize
  154. init();
  155.  
  156. // make global
  157. window.domReady = domReady;
  158.  
  159. })(window);

Report this snippet  

You need to login to post a comment.