domReady.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license RequireJS domReady 2.0.1 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
  3. * Available via the MIT or new BSD license.
  4. * see: http://github.com/requirejs/domReady for details
  5. */
  6. /*jslint */
  7. /*global require: false, define: false, requirejs: false,
  8. window: false, clearInterval: false, document: false,
  9. self: false, setInterval: false */
  10. define(function () {
  11. 'use strict';
  12. var isTop, testDiv, scrollIntervalId,
  13. isBrowser = typeof window !== "undefined" && window.document,
  14. isPageLoaded = !isBrowser,
  15. doc = isBrowser ? document : null,
  16. readyCalls = [];
  17. function runCallbacks(callbacks) {
  18. var i;
  19. for (i = 0; i < callbacks.length; i += 1) {
  20. callbacks[i](doc);
  21. }
  22. }
  23. function callReady() {
  24. var callbacks = readyCalls;
  25. if (isPageLoaded) {
  26. //Call the DOM ready callbacks
  27. if (callbacks.length) {
  28. readyCalls = [];
  29. runCallbacks(callbacks);
  30. }
  31. }
  32. }
  33. /**
  34. * Sets the page as loaded.
  35. */
  36. function pageLoaded() {
  37. if (!isPageLoaded) {
  38. isPageLoaded = true;
  39. if (scrollIntervalId) {
  40. clearInterval(scrollIntervalId);
  41. }
  42. callReady();
  43. }
  44. }
  45. if (isBrowser) {
  46. if (document.addEventListener) {
  47. //Standards. Hooray! Assumption here that if standards based,
  48. //it knows about DOMContentLoaded.
  49. document.addEventListener("DOMContentLoaded", pageLoaded, false);
  50. window.addEventListener("load", pageLoaded, false);
  51. } else if (window.attachEvent) {
  52. window.attachEvent("onload", pageLoaded);
  53. testDiv = document.createElement('div');
  54. try {
  55. isTop = window.frameElement === null;
  56. } catch (e) {}
  57. //DOMContentLoaded approximation that uses a doScroll, as found by
  58. //Diego Perini: http://javascript.nwbox.com/IEContentLoaded/,
  59. //but modified by other contributors, including jdalton
  60. if (testDiv.doScroll && isTop && window.external) {
  61. scrollIntervalId = setInterval(function () {
  62. try {
  63. testDiv.doScroll();
  64. pageLoaded();
  65. } catch (e) {}
  66. }, 30);
  67. }
  68. }
  69. //Check if document already complete, and if so, just trigger page load
  70. //listeners. Latest webkit browsers also use "interactive", and
  71. //will fire the onDOMContentLoaded before "interactive" but not after
  72. //entering "interactive" or "complete". More details:
  73. //http://dev.w3.org/html5/spec/the-end.html#the-end
  74. //http://stackoverflow.com/questions/3665561/document-readystate-of-interactive-vs-ondomcontentloaded
  75. //Hmm, this is more complicated on further use, see "firing too early"
  76. //bug: https://github.com/requirejs/domReady/issues/1
  77. //so removing the || document.readyState === "interactive" test.
  78. //There is still a window.onload binding that should get fired if
  79. //DOMContentLoaded is missed.
  80. if (document.readyState === "complete") {
  81. pageLoaded();
  82. }
  83. }
  84. /** START OF PUBLIC API **/
  85. /**
  86. * Registers a callback for DOM ready. If DOM is already ready, the
  87. * callback is called immediately.
  88. * @param {Function} callback
  89. */
  90. function domReady(callback) {
  91. if (isPageLoaded) {
  92. callback(doc);
  93. } else {
  94. readyCalls.push(callback);
  95. }
  96. return domReady;
  97. }
  98. domReady.version = '2.0.1';
  99. /**
  100. * Loader Plugin API method
  101. */
  102. domReady.load = function (name, req, onLoad, config) {
  103. if (config.isBuild) {
  104. onLoad(null);
  105. } else {
  106. domReady(onLoad);
  107. }
  108. };
  109. /** END OF PUBLIC API **/
  110. return domReady;
  111. });