ready-no-deferred.js 2.2 KB

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