Skip to content

Instantly share code, notes, and snippets.

@jlewin
Created January 9, 2013 08:37
Show Gist options
  • Save jlewin/4491613 to your computer and use it in GitHub Desktop.
Save jlewin/4491613 to your computer and use it in GitHub Desktop.
(function () {
var el = document.createElement('div'),
b = document.getElementsByTagName('body')[0],
otherlib = false;
// Message window element
el.style.position = 'fixed';
el.style.height = '32px';
el.style.width = '220px';
el.style.marginLeft = '-110px';
el.style.top = '0';
el.style.left = '50%';
el.style.padding = '5px 10px';
el.style.zIndex = 1001;
el.style.fontSize = '12px';
el.style.color = '#222';
el.style.backgroundColor = '#f99';
// Determine if jQuery is loaded and/or if another function is already bound to $
if (typeof jQuery != 'undefined')
return showMsg('This page already using jQuery v' + jQuery.fn.jquery);
else if (typeof $ == 'function')
otherlib = true;
// Script loader - more or less stolen form jquery core and adapted by paul irish
function loadScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState
|| this.readyState == 'loaded'
|| this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
// Load jQuery
loadScript('http://code.jquery.com/jquery-latest.min.js', function () {
if (typeof jQuery == 'undefined')
showMsg('Sorry, but jQuery wasn\'t able to load');
else
showMsg('This page is now jQuerified with v' + jQuery.fn.jquery + (otherlib ? ' and noConflict(). Use $jq(), not $().' : ''));
});
// Show status message
function showMsg(msg) {
el.innerHTML = msg;
b.appendChild(el);
window.setTimeout(function () {
if (typeof jQuery == 'undefined') {
b.removeChild(el);
} else {
jQuery(el).fadeOut('slow', function () {
jQuery(this).remove();
});
if (otherlib)
$jq = jQuery.noConflict();
}
}, 2500);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment