5 ], function( jQuery ) {
7 // The deferred used on DOM ready
10 jQuery.fn.ready = function( fn ) {
12 jQuery.ready.promise().done( fn );
18 // Is the DOM ready to be used? Set to true once it occurs.
21 // A counter to track how many items to wait for before
22 // the ready event fires. See #6781
25 // Hold (or release) the ready event
26 holdReady: function( hold ) {
34 // Handle when the DOM is ready
35 ready: function( wait ) {
37 // Abort if there are pending holds or we're already ready
38 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
42 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
43 if ( !document.body ) {
44 return setTimeout( jQuery.ready );
47 // Remember that the DOM is ready
48 jQuery.isReady = true;
50 // If a normal DOM Ready event fired, decrement, and wait if need be
51 if ( wait !== true && --jQuery.readyWait > 0 ) {
55 // If there are functions bound, to execute
56 readyList.resolveWith( document, [ jQuery ] );
58 // Trigger any bound ready events
59 if ( jQuery.fn.triggerHandler ) {
60 jQuery( document ).triggerHandler( "ready" );
61 jQuery( document ).off( "ready" );
67 * Clean-up method for dom ready events
70 if ( document.addEventListener ) {
71 document.removeEventListener( "DOMContentLoaded", completed, false );
72 window.removeEventListener( "load", completed, false );
75 document.detachEvent( "onreadystatechange", completed );
76 window.detachEvent( "onload", completed );
81 * The ready event handler and self cleanup method
83 function completed() {
84 // readyState === "complete" is good enough for us to call the dom ready in oldIE
85 if ( document.addEventListener ||
86 event.type === "load" ||
87 document.readyState === "complete" ) {
94 jQuery.ready.promise = function( obj ) {
97 readyList = jQuery.Deferred();
99 // Catch cases where $(document).ready() is called
100 // after the browser event has already occurred.
101 // we once tried to use readyState "interactive" here,
102 // but it caused issues like the one
103 // discovered by ChrisS here:
104 // http://bugs.jquery.com/ticket/12282#comment:15
105 if ( document.readyState === "complete" ) {
106 // Handle it asynchronously to allow scripts the opportunity to delay ready
107 setTimeout( jQuery.ready );
109 // Standards-based browsers support DOMContentLoaded
110 } else if ( document.addEventListener ) {
111 // Use the handy event callback
112 document.addEventListener( "DOMContentLoaded", completed, false );
114 // A fallback to window.onload, that will always work
115 window.addEventListener( "load", completed, false );
117 // If IE event model is used
119 // Ensure firing before onload, maybe late but safe also for iframes
120 document.attachEvent( "onreadystatechange", completed );
122 // A fallback to window.onload, that will always work
123 window.attachEvent( "onload", completed );
125 // If IE and not a frame
126 // continually check to see if the document is ready
130 top = window.frameElement == null && document.documentElement;
133 if ( top && top.doScroll ) {
134 (function doScrollCheck() {
135 if ( !jQuery.isReady ) {
138 // Use the trick by Diego Perini
139 // http://javascript.nwbox.com/IEContentLoaded/
140 top.doScroll("left");
142 return setTimeout( doScrollCheck, 50 );
145 // detach all dom ready events
148 // and execute any waiting functions
155 return readyList.promise( obj );