Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Last active June 3, 2016 22:32
Show Gist options
  • Save JDMcKinstry/cfc32292cbbfa548fb9584db05b2b2fc to your computer and use it in GitHub Desktop.
Save JDMcKinstry/cfc32292cbbfa548fb9584db05b2b2fc to your computer and use it in GitHub Desktop.
;(function() {
var hrefUI = { open: "https://ajax.googleapis.com/ajax/libs/jqueryui/", close: "/themes/smoothness/jquery-ui.css" },
srcJQ = { open: "https://ajax.googleapis.com/ajax/libs/jquery/", close: "/jquery.min.js" },
srcUI = { open: "https://ajax.googleapis.com/ajax/libs/jqueryui/", close: "/jquery-ui.min.js" },
jQVers = ["2.2.4","2.2.3","2.2.2","2.2.1","2.2.0","2.1.4","2.1.3","2.1.1","2.1.0","2.0.3","2.0.2","2.0.1","2.0.0","1.12.4","1.12.3","1.12.2","1.12.1","1.12.0","1.11.3","1.11.2","1.11.1","1.11.0","1.10.2","1.10.1","1.10.0","1.9.1","1.9.0","1.8.3","1.8.2","1.8.1","1.8.0","1.7.2","1.7.1","1.7.0","1.6.4","1.6.3","1.6.2","1.6.1","1.6.0","1.5.2","1.5.1","1.5.0","1.4.4","1.4.3","1.4.2","1.4.1","1.4.0","1.3.2","1.3.1","1.3.0","1.2.6","1.2.3"],
jQUIVers = ["1.11.4","1.11.3","1.11.2","1.11.1","1.11.0","1.10.4","1.10.3","1.10.2","1.10.1","1.10.0","1.9.2","1.9.1","1.9.0","1.8.24","1.8.23","1.8.22","1.8.21","1.8.20","1.8.19","1.8.18","1.8.17","1.8.16","1.8.15","1.8.14","1.8.13","1.8.12","1.8.11","1.8.10","1.8.9","1.8.8","1.8.7","1.8.6","1.8.5","1.8.4","1.8.2","1.8.1","1.8.0","1.7.3","1.7.2","1.7.1","1.7.0","1.6.0","1.5.3","1.5.2"],
classes = {
lnk: 'switcher-ui-link',
js: 'switcher-script',
jsUI: 'switcher-ui-script',
selJQ: 'switcher',
selJQUI: 'switcher-ui'
};
function getHREFUI(ver) { return hrefUI.open + ver + hrefUI.close; }
function getSRCUI(ver) { return srcUI.open + ver + srcUI.close; }
function getSRCJQ(ver) { return srcJQ.open + ver + srcJQ.close; }
function jQuerySwitcher() {
var sel = document.createElement('select');
sel.setAttribute('class', classes.selJQ);
sel.style = "bottom:1em;left:1em;position:fixed;z-index=9999"
for (var i=0;i<jQVers.length;i++) {
var opt = document.createElement('option');
opt.text = "jQuery UI (" + jQVers[i] + ")";
opt.value = jQVers[i];
sel.appendChild(opt);
}
sel.getSRCJQ = getSRCJQ;
sel.findJQScript = findJQScript;
sel.onchange = eventOnChangeSwitcher;
//js = getJQScript();
//js.onload = eventOnLoadJQ;
document.body.appendChild(sel);
return sel;
}
function jQueryUISwitcher() {
var sel = document.createElement('select');
sel.setAttribute('class', classes.selJQUI);
sel.style = "bottom:1em;position:fixed;right:1em;z-index=9999"
for (var i=0;i<jQUIVers.length;i++) {
var opt = document.createElement('option');
opt.text = "jQuery UI (" + jQUIVers[i] + ")";
opt.value = jQUIVers[i];
sel.appendChild(opt);
}
sel.getHREFUI = getHREFUI;
sel.getSRCUI = getSRCUI;
sel.findJQUILink = findJQUILink;
sel.findJQUIScript = findJQUIScript;
sel.onchange = eventOnChangeUISwitcher;
//lnk = getJQUILink();
//lnk.onload = eventOnLoadJQUILink;
//js = getJQUIScript();
//js.onload = eventOnLoadJQUI;
document.body.appendChild(sel);
return sel;
}
function eventOnChangeSwitcher(e) {
var ver = this.value,
js = getJQScript();
js.src = getSRCJQ(ver);
}
function eventOnChangeUISwitcher(e) {
var ver = this.value,
lnk = getJQUILink(),
js = getJQUIScript();
//if (jQuery && jQuery.hasOwnProperty('ui')) delete jQuery['ui'];
lnk.href = getHREFUI(ver);
js.src = getSRCUI(ver);
}
function eventOnLoadJQ(e) {
try {
console.log("Active jQuery version: " + jQuery.fn.jquery);
}
catch(err) {}
}
function eventOnLoadJQUI(e) {
try {
console.log("Active jQuery UI version: " + jQuery.ui.version);
}
catch(err) {}
}
function eventOnLoadJQUILink(e) {
try {
console.log(e)
}
catch(err) {}
}
function createJQUILink(ver) {
if (void 0 == ver || jQUIVers.indexOf(ver) == -1) ver = jQUIVers[0];
var lnk = document.createElement('link');
lnk.rel = "stylesheet";
lnk.href = getHREFUI(ver);
lnk.setAttribute('class', 'switcher-ui-link');
document.getElementsByTagName('head')[0].appendChild(lnk);
return lnk;
}
function createJQScript(ver) {
if (void 0 == ver || jQVers.indexOf(ver) == -1) ver = jQVers[0];
var js = document.createElement('script');
js.type = "text/javascript";
js.src = getSRCJQ(ver);
js.setAttribute('class', classes.js);
document.getElementsByTagName('head')[0].appendChild(js);
return js;
}
function createJQUIScript(ver) {
if (void 0 == ver || jQUIVers.indexOf(ver) == -1) ver = jQUIVers[0];
var js = document.createElement('script');
js.type = "text/javascript";
js.src = getSRCUI(ver);
js.setAttribute('class', classes.jsUI);
document.getElementsByTagName('head')[0].appendChild(js);
return js;
}
function findJQUILink() {
var h = document.getElementsByTagName('head')[0],
c = h.childNodes;
for (var x in c) {
if (c[x] instanceof Element && c[x].tagName.toLowerCase() == "link") {
var href = c[x].href;
if (/jquery/i.test(href) && /ui/i.test(href)) return c[x];
}
}
return void 0;
}
function findJQScript() {
var h = document.getElementsByTagName('head')[0],
c = h.childNodes;
for (var x in c) {
if (c[x] instanceof Element && c[x].tagName.toLowerCase() == "script") {
var src = c[x].src;
if (/jquery/i.test(src) && !(/ui/i.test(src))) return c[x];
}
}
return void 0;
}
function findJQUIScript() {
var h = document.getElementsByTagName('head')[0],
c = h.childNodes;
for (var x in c) {
if (c[x] instanceof Element && c[x].tagName.toLowerCase() == "script") {
var src = c[x].src;
if (/jquery/i.test(src) && /ui/i.test(src)) return c[x];
}
}
return void 0;
}
function getJQUILink() {
lnk = findJQUILink() || createJQUILink();
if (lnk.classList && !hasClass(lnk, classes.lnk)) lnk.classList.add(classes.lnk);
return lnk;
}
function getJQScript() {
js = findJQScript() || createJQScript();
if (js.classList && !hasClass(js, classes.js)) js.classList.add(classes.js);
return js;
}
function getJQUIScript() {
js = findJQUIScript() || createJQUIScript();
if (js.classList && !hasClass(js, classes.jsUI)) js.classList.add(classes.jsUI);
return js;
}
function hasClass(el, cls) {
return el.className.indexOf(cls) != -1;
}
function init() {
window.jQuerySwitcher = new jQuerySwitcher();
window.jQueryUISwitcher = new jQueryUISwitcher();
}
if (document.readyState == "complete") init();
else window.onload = init;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment