Skip to content

Instantly share code, notes, and snippets.

@nathanhammond
Created April 7, 2011 22:13
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 nathanhammond/908885 to your computer and use it in GitHub Desktop.
Save nathanhammond/908885 to your computer and use it in GitHub Desktop.
$loader (pronounced "money-loader") is a simple script for using either the embedded version of jQuery or loading your own copy but saving a reference in your own scope. The primary use-case for this is in development of code that will be embedded into ot
// Load jQuery.
var $loader = function(callback) {
this.callback = callback;
this.check$();
}
$loader.prototype = {
// Check to see if we have our own copy of jQuery.
check$: function(version) {
// Load event version.
// TODO: Limit to our earliest supported version of jQuery.
if (window.jQuery) {
// jQuery is already in existence, use their copy.
this.have$(true);
} else {
// First call and jQuery doesn't exist, load us a copy.
this.load$(version);
}
// Timeout version.
/*
if (first && window.jQuery) {
// jQuery is already in existence, use their copy.
// TODO: Limit to our earliest supported version of jQuery.
this.have$(first);
} else if (first) {
// First call and jQuery doesn't exist, load us a copy.
this.load$();
} else if (window.jQuery) {
// We've loaded our own jQuery, got here via a setTimeout call.
this.have$();
} else {
setTimeout(this.arguments.callee, 200);
}
*/
},
// Get our own copy of jQuery.
load$: function(version) {
var self = this;
version = version || '1.5.2'
// Build our script tag.
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'ajax.googleapis.com/ajax/libs/jquery/'+version+'/jquery.min.js';
// Load event version.
s.onload = function() {
self.have$();
}
s.onreadystatechange = function() {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
self.have$();
}
}
var o = document.getElementsByTagName('script')[0];
o.parentNode.insertBefore(s, o);
},
// Save a reference for ourselves!
have$: function(first) {
// Pass the reference to the callback.
var reference;
if (first) {
// We're using their copy.
reference = window.jQuery;
} else {
// We're using our copy, get jQuery back out of their global scope.
reference = window.jQuery.noConflict(true);
}
this.callback(reference);
}
};
// Usage
new $loader(function(reference) {
// Intentionally leaking $, it should be defined in the correct (private) scope.
$ = reference;
});

$loader (pronounced "money-loader") is a simple script for using either the embedded version of jQuery or loading your own copy but saving a reference in your own scope. The primary use-case for this is in development of code that will be embedded into others' sites.

If you have multiple chained dependencies (other than just jQuery) I instead recommend embedding LABjs and modifying it to leak $LAB from its own scope into yours.

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