Skip to content

Instantly share code, notes, and snippets.

@Boldewyn
Created May 13, 2011 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boldewyn/970473 to your computer and use it in GitHub Desktop.
Save Boldewyn/970473 to your computer and use it in GitHub Desktop.
jQuery deferring helper
var $=(function(){var x=[],z=setInterval(function(){if("jQuery" in window){$.each(x,function(){$(this)});clearInterval(z)}},10);return function(y){x.push(y)}})();
/* uncompressed: */
var $ = (function() {
var x = [],
z = setInterval(function() {
if ("jQuery" in window) {
$.each(x, function() {
$(this);
});
clearInterval(z);
}
}, 10);
return function (y) {
x.push(y);
};
})();
@Boldewyn
Copy link
Author

If you use defer="defer" on jQuery, the $ variable isn't defined, until jQuery has loaded. To be able to embed $(function(){})-style code though, this little helper (162B) will take care of defining a fallback $ until this happens.

@Boldewyn
Copy link
Author

By the way, quite the same solution in an 2008 jQuery list posting:
http://groups.google.com/group/jquery-en/browse_thread/thread/f31d8cf3ec8cd55a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment