ready-no-deferred.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. define( [
  2. "../core",
  3. "../var/document"
  4. ], function( jQuery, document ) {
  5. "use strict";
  6. var readyCallbacks = [],
  7. readyFiring = false,
  8. whenReady = function( fn ) {
  9. readyCallbacks.push( fn );
  10. },
  11. executeReady = function( fn ) {
  12. // Prevent errors from freezing future callback execution (gh-1823)
  13. // Not backwards-compatible as this does not execute sync
  14. window.setTimeout( function() {
  15. fn.call( document, jQuery );
  16. } );
  17. };
  18. jQuery.fn.ready = function( fn ) {
  19. whenReady( fn );
  20. return this;
  21. };
  22. jQuery.extend( {
  23. // Is the DOM ready to be used? Set to true once it occurs.
  24. isReady: false,
  25. // A counter to track how many items to wait for before
  26. // the ready event fires. See #6781
  27. readyWait: 1,
  28. // Hold (or release) the ready event
  29. holdReady: function( hold ) {
  30. if ( hold ) {
  31. jQuery.readyWait++;
  32. } else {
  33. jQuery.ready( true );
  34. }
  35. },
  36. ready: function( wait ) {
  37. // Abort if there are pending holds or we're already ready
  38. if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
  39. return;
  40. }
  41. // Remember that the DOM is ready
  42. jQuery.isReady = true;
  43. // If a normal DOM Ready event fired, decrement, and wait if need be
  44. if ( wait !== true && --jQuery.readyWait > 0 ) {
  45. return;
  46. }
  47. whenReady = function( fn ) {
  48. readyCallbacks.push( fn );
  49. if ( !readyFiring ) {
  50. readyFiring = true;
  51. while ( readyCallbacks.length ) {
  52. fn = readyCallbacks.shift();
  53. if ( jQuery.isFunction( fn ) ) {
  54. executeReady( fn );
  55. }
  56. }
  57. readyFiring = false;
  58. }
  59. };
  60. whenReady();
  61. }
  62. } );
  63. // Make jQuery.ready Promise consumable (gh-1778)
  64. jQuery.ready.then = jQuery.fn.ready;
  65. /**
  66. * The ready event handler and self cleanup method
  67. */
  68. function completed() {
  69. document.removeEventListener( "DOMContentLoaded", completed );
  70. window.removeEventListener( "load", completed );
  71. jQuery.ready();
  72. }
  73. // Catch cases where $(document).ready() is called
  74. // after the browser event has already occurred.
  75. // Support: IE9-10 only
  76. // Older IE sometimes signals "interactive" too soon
  77. if ( document.readyState === "complete" ||
  78. ( document.readyState !== "loading" && !document.documentElement.doScroll ) ) {
  79. // Handle it asynchronously to allow scripts the opportunity to delay ready
  80. window.setTimeout( jQuery.ready );
  81. } else {
  82. // Use the handy event callback
  83. document.addEventListener( "DOMContentLoaded", completed );
  84. // A fallback to window.onload, that will always work
  85. window.addEventListener( "load", completed );
  86. }
  87. } );