ready.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. define( [
  2. "../core",
  3. "../var/document",
  4. "../deferred"
  5. ], function( jQuery, document ) {
  6. "use strict";
  7. // The deferred used on DOM ready
  8. var readyList = jQuery.Deferred();
  9. jQuery.fn.ready = function( fn ) {
  10. readyList.then( fn );
  11. return this;
  12. };
  13. jQuery.extend( {
  14. // Is the DOM ready to be used? Set to true once it occurs.
  15. isReady: false,
  16. // A counter to track how many items to wait for before
  17. // the ready event fires. See #6781
  18. readyWait: 1,
  19. // Hold (or release) the ready event
  20. holdReady: function( hold ) {
  21. if ( hold ) {
  22. jQuery.readyWait++;
  23. } else {
  24. jQuery.ready( true );
  25. }
  26. },
  27. // Handle when the DOM is ready
  28. ready: function( wait ) {
  29. // Abort if there are pending holds or we're already ready
  30. if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
  31. return;
  32. }
  33. // Remember that the DOM is ready
  34. jQuery.isReady = true;
  35. // If a normal DOM Ready event fired, decrement, and wait if need be
  36. if ( wait !== true && --jQuery.readyWait > 0 ) {
  37. return;
  38. }
  39. // If there are functions bound, to execute
  40. readyList.resolveWith( document, [ jQuery ] );
  41. }
  42. } );
  43. jQuery.ready.then = readyList.then;
  44. // The ready event handler and self cleanup method
  45. function completed() {
  46. document.removeEventListener( "DOMContentLoaded", completed );
  47. window.removeEventListener( "load", completed );
  48. jQuery.ready();
  49. }
  50. // Catch cases where $(document).ready() is called
  51. // after the browser event has already occurred.
  52. // Support: IE <=9 - 10 only
  53. // Older IE sometimes signals "interactive" too soon
  54. if ( document.readyState === "complete" ||
  55. ( document.readyState !== "loading" && !document.documentElement.doScroll ) ) {
  56. // Handle it asynchronously to allow scripts the opportunity to delay ready
  57. window.setTimeout( jQuery.ready );
  58. } else {
  59. // Use the handy event callback
  60. document.addEventListener( "DOMContentLoaded", completed );
  61. // A fallback to window.onload, that will always work
  62. window.addEventListener( "load", completed );
  63. }
  64. } );