Build: update grunt-jscs-checker and pass with the new rules
[jquery.git] / src / core / ready.js
blob095e6daeb0a5205a7b832faa3a8cfb9e11ca7f9d
1 define([
2         "../core",
3         "../core/init",
4         "../deferred"
5 ], function( jQuery ) {
7 // The deferred used on DOM ready
8 var readyList;
10 jQuery.fn.ready = function( fn ) {
11         // Add the callback
12         jQuery.ready.promise().done( fn );
14         return this;
17 jQuery.extend({
18         // Is the DOM ready to be used? Set to true once it occurs.
19         isReady: false,
21         // A counter to track how many items to wait for before
22         // the ready event fires. See #6781
23         readyWait: 1,
25         // Hold (or release) the ready event
26         holdReady: function( hold ) {
27                 if ( hold ) {
28                         jQuery.readyWait++;
29                 } else {
30                         jQuery.ready( true );
31                 }
32         },
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 ) {
39                         return;
40                 }
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 );
45                 }
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 ) {
52                         return;
53                 }
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" );
62                 }
63         }
64 });
66 /**
67  * Clean-up method for dom ready events
68  */
69 function detach() {
70         if ( document.addEventListener ) {
71                 document.removeEventListener( "DOMContentLoaded", completed, false );
72                 window.removeEventListener( "load", completed, false );
74         } else {
75                 document.detachEvent( "onreadystatechange", completed );
76                 window.detachEvent( "onload", completed );
77         }
80 /**
81  * The ready event handler and self cleanup method
82  */
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" ) {
89                 detach();
90                 jQuery.ready();
91         }
94 jQuery.ready.promise = function( obj ) {
95         if ( !readyList ) {
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
118                 } else {
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
127                         var top = false;
129                         try {
130                                 top = window.frameElement == null && document.documentElement;
131                         } catch ( e ) {}
133                         if ( top && top.doScroll ) {
134                                 (function doScrollCheck() {
135                                         if ( !jQuery.isReady ) {
137                                                 try {
138                                                         // Use the trick by Diego Perini
139                                                         // http://javascript.nwbox.com/IEContentLoaded/
140                                                         top.doScroll("left");
141                                                 } catch ( e ) {
142                                                         return setTimeout( doScrollCheck, 50 );
143                                                 }
145                                                 // detach all dom ready events
146                                                 detach();
148                                                 // and execute any waiting functions
149                                                 jQuery.ready();
150                                         }
151                                 })();
152                         }
153                 }
154         }
155         return readyList.promise( obj );