Last active
December 17, 2015 20:29
-
-
Save jbenner-radham/5667515 to your computer and use it in GitHub Desktop.
A quick and dirty client side script to check directory depth, build a relative path and add a JQuery script element. After the first self-executing anonymous function a second fires with a slight delay (if JQuery isn't yet initialized) to verify that JQuery is loaded & ready to rock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
// Pt. I - Put this near the top of the head to append JQuery (In case you need doc.ready or whatnot). | |
var pageDepth = 0, relPath; | |
for (var path = location.pathname, len = path.length, c = 0; c < len; ++c) | |
if (path[c] === '/') | |
pageDepth += 1; | |
if (pageDepth > 1) | |
for (var i = 0; i < pageDepth; relPath += '../', ++i); | |
else | |
relPath = './'; | |
var headH = document.getElementsByTagName('head')[0]; | |
// JQuery | |
var jqueryTag = document.createElement('script'); | |
jqueryTag.src = relPath + 'js/lib/jquery.min.js'; | |
headH.appendChild(jqueryTag); | |
})(); | |
(function(window) { | |
// Pt. II - Put this near the bottom of your page (or wherever you like) to do the magic. | |
var jqueryChk = function() { | |
this.pass = this.pass || 0; | |
// Check if JQuery is undefined and if it is recheck | |
// 499 more times before the recursive call bails. | |
if (typeof $ === 'undefined' && this.pass++ < 500) { | |
setTimeout(jqueryChk, 10); | |
} else { | |
// Do JQuery ninja stuff here... | |
} | |
}; | |
jqueryChk(); | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment