Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created January 11, 2013 19:17
Show Gist options
  • Save srstanic/4513196 to your computer and use it in GitHub Desktop.
Save srstanic/4513196 to your computer and use it in GitHub Desktop.
Dom manipulation functions useful for dynamically loading scripts.
window.dom = {
createScriptElement: function(url, async) {
var script = document.createElement("script");
script.setAttribute("src", url);
if (async !== undefined) {
script.async = async;
}
return script;
},
findScriptElementById: function(id) {
var foundScript;
for(var i=0, len=document.scripts.length; i<len; i++) {
script = document.scripts[i];
if (script.id === id){
foundScript = script;
break;
}
}
return foundScript;
},
appendAfter: function(referenceNode, newNode) {
if (referenceNode && newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
} else {
console.log("Tried to append " + newNode + " after " + referenceNode);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment