Last active
December 20, 2015 03:09
-
-
Save dexygen/6061573 to your computer and use it in GitHub Desktop.
Genuinely Unobtrusive Javascript
This file contains hidden or 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(scope) { | |
function destroyAutoGlobals(options) { | |
var allElements = document.getElementsByTagName("*"), elementId; | |
for (var i=allElements.length; i--; ) { | |
elementId = allElements[i].id; | |
if (elementId && window[elementId] instanceof HTMLElement) { | |
options && options.verbose && console.log('Destroying window["' + elementId + '"]'); | |
window[elementId] = null; | |
} | |
} | |
} | |
function guid() { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | |
return v.toString(16); | |
}); | |
} | |
function load(scripts) { | |
var runLast = scripts.pop(), | |
runBefore = scripts.length; | |
for (var i=runBefore; i--; ) { | |
appendScript(scripts[i]); | |
} | |
function appendScript(scriptSrc) { | |
var scriptEl = document.createElement('script') | |
scriptEl.src = scriptSrc; | |
scriptEl.async = true; | |
scriptEl.onload = function() { | |
if (!--runBefore) { | |
appendScript(runLast); | |
} | |
}; | |
document.head.appendChild(scriptEl); | |
} | |
} | |
var MEEK = scope.MEEK = scope.MEEK || { | |
appId: (function() { | |
var appId; | |
return function() { | |
return appId || (function() { | |
appId = guid(); | |
return appId; | |
})(); | |
} | |
})(), | |
destroyAutoGlobals: destroyAutoGlobals, | |
guid: guid, | |
load: load | |
}; | |
})(this); | |
(function(){ | |
/* | |
* <script src="http://jsfiddle.net/dexygen/vFseA/show_js/"> | |
* <script src="http://jsfiddle.net/dexygen/APERM/show_js/"> | |
* <script src="http://jsfiddle.net/dexygen/7G8pX/show_js"> | |
* | |
* The above (with closing script tags of course) will run EXACTLY THE SAME as the following | |
*/ | |
var scripts = [ | |
'http://jsfiddle.net/dexygen/vFseA/show_js/', | |
'http://jsfiddle.net/dexygen/APERM/show_js/', | |
'http://jsfiddle.net/dexygen/7G8pX/show_js/' //runs code loaded in previous scripts | |
]; | |
MEEK.load(scripts); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment