Release: push dist to same remote as project
[jquery.git] / src / core / ready.js
blobcc812e6ac0a168fcacb30d410c8279c749354e96
1 define([
2 "../core",
3 "../var/document",
4 "../core/init",
5 "../deferred"
6 ], function( jQuery, document ) {
8 // The deferred used on DOM ready
9 var readyList;
11 jQuery.fn.ready = function( fn ) {
12 // Add the callback
13 jQuery.ready.promise().done( fn );
15 return this;
18 jQuery.extend({
19 // Is the DOM ready to be used? Set to true once it occurs.
20 isReady: false,
22 // A counter to track how many items to wait for before
23 // the ready event fires. See #6781
24 readyWait: 1,
26 // Hold (or release) the ready event
27 holdReady: function( hold ) {
28 if ( hold ) {
29 jQuery.readyWait++;
30 } else {
31 jQuery.ready( true );
35 // Handle when the DOM is ready
36 ready: function( wait ) {
38 // Abort if there are pending holds or we're already ready
39 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
40 return;
43 // Remember that the DOM is ready
44 jQuery.isReady = true;
46 // If a normal DOM Ready event fired, decrement, and wait if need be
47 if ( wait !== true && --jQuery.readyWait > 0 ) {
48 return;
51 // If there are functions bound, to execute
52 readyList.resolveWith( document, [ jQuery ] );
54 // Trigger any bound ready events
55 if ( jQuery.fn.triggerHandler ) {
56 jQuery( document ).triggerHandler( "ready" );
57 jQuery( document ).off( "ready" );
60 });
62 /**
63 * The ready event handler and self cleanup method
65 function completed() {
66 document.removeEventListener( "DOMContentLoaded", completed, false );
67 window.removeEventListener( "load", completed, false );
68 jQuery.ready();
71 jQuery.ready.promise = function( obj ) {
72 if ( !readyList ) {
74 readyList = jQuery.Deferred();
76 // Catch cases where $(document).ready() is called
77 // after the browser event has already occurred.
78 // We once tried to use readyState "interactive" here,
79 // but it caused issues like the one
80 // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
81 if ( document.readyState === "complete" ) {
82 // Handle it asynchronously to allow scripts the opportunity to delay ready
83 setTimeout( jQuery.ready );
85 } else {
87 // Use the handy event callback
88 document.addEventListener( "DOMContentLoaded", completed, false );
90 // A fallback to window.onload, that will always work
91 window.addEventListener( "load", completed, false );
94 return readyList.promise( obj );
97 // Kick off the DOM ready check even if the user does not
98 jQuery.ready.promise();