|
// 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; |
|
}); |