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