Skip to content

Commit

Permalink
Fix for DOM Ready bug in jsdefer.js
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisMoore committed Apr 25, 2011
1 parent fb993ed commit 4afe68f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
3 changes: 3 additions & 0 deletions demos/feature-tests/basic/1a_delay-domready.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
})
.done( log );

$( function() {
log( "DOM Ready" );
});
</script>
</head>
<body>
Expand Down
3 changes: 3 additions & 0 deletions demos/feature-tests/basic/1b_delay-domready.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
})
.done( log );

$( function() {
log( "DOM Ready" );
});
</script>
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions jquery.defer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var document = window.document,
loadingSubScripts;

function absUrl( basePath, url ) {
if ( url.indexOf( "://") === -1 ) {
if ( url.indexOf( "//" ) === -1 ) {
url = basePath + url;
}
return anchor.href = url;
Expand Down Expand Up @@ -44,7 +44,7 @@ function normalize( items, basePath ) {
function getScriptDef( name, thisUrl ) {
var thisUrlKey,
scriptDef = defer[ name ];

if ( scriptDef ) {
return scriptDef;
}
Expand Down
36 changes: 18 additions & 18 deletions jsdefer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ var $, document = window.document,
loadingScripts = [],
loadingSubScripts,
promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
slice = Array.prototype.slice,
sliceDeferred = [].slice;
slice = [].slice;

if ( window.jQuery ) {
////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -200,12 +199,12 @@ if ( window.jQuery ) {
$.Deferred();
function resolveFunc( i ) {
return function( value ) {
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
args[ i ] = arguments.length > 1 ? slice.call( arguments, 0 ) : value;
if ( !( --count ) ) {
// Strange bug in FF4:
// Values changed onto the arguments object sometimes end up as undefined values
// outside the $.when method. Cloning the object into a fresh array solves the issue
deferred.resolveWith( deferred, sliceDeferred.call( args, 0 ) );
deferred.resolveWith( deferred, slice.call( args, 0 ) );
}
};
}
Expand Down Expand Up @@ -253,25 +252,14 @@ if ( window.jQuery ) {
head.insertBefore( script, head.firstChild );
return deferred;
};

function domReady() {
if ( !document.body ) {
return setTimeout( function() {
domReady();
}, 1 );
}
$.isReady = true;
ready( true );
}
domReady();
}


////////////////////////////////////////////////////////////////////////////////////////////////
// The following code is identical to the corresponding code in jquery.defer.js

function absUrl( basePath, url ) {
if ( url.indexOf( "://") === -1 ) {
if ( url.indexOf( "://" ) === -1 ) {
url = basePath + url;
}
return anchor.href = url;
Expand Down Expand Up @@ -304,7 +292,7 @@ function normalize( items, basePath ) {
function getScriptDef( name, thisUrl ) {
var thisUrlKey,
scriptDef = defer[ name ];

if ( scriptDef ) {
return scriptDef;
}
Expand Down Expand Up @@ -425,7 +413,7 @@ $.extend({
reject();
}
// Non-wrapped script
if ( jQuery && $ !== jQuery ) {
if ( window.jQuery && $ !== jQuery ) {
// Special case: jQuery has been loaded dynamically by JsDefer, so switch to plugin version of JsDefer
$ = jQuery.extend({
defer: defer,
Expand Down Expand Up @@ -585,4 +573,16 @@ ready = $.ready;
readyList = $.Deferred();
readyList.promise( ready );

if ( !window.jQuery ) {
function domReady() {
if ( !document.body ) {
return setTimeout( function() {
domReady();
}, 1 );
}
$.isReady = true;
$.ready( true );
}
domReady();
}
})( window );
2 changes: 1 addition & 1 deletion jsdefer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4afe68f

Please sign in to comment.