Created
December 22, 2011 08:24
-
-
Save n1k0/1509496 to your computer and use it in GitHub Desktop.
Simple javascript include
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 include(url) { | |
var element; | |
switch (url.split(".").pop()) { | |
case "css": | |
{ | |
element = document.createElement("link"); | |
element.setAttribute("rel", "stylesheet"); | |
element.setAttribute("type", "text/css") | |
element.setAttribute("href", url) | |
} | |
break; | |
case "js": | |
{ | |
element = document.createElement("script"); | |
element.setAttribute("language", "javascript") | |
element.setAttribute("src", url) | |
} | |
break; | |
default: | |
window.console && window.console.error("could not identify", url, "skip include"); | |
return; | |
} | |
var head = document.querySelector("head"); | |
if (head.innerHTML.indexOf(element.outerHTML) != -1) { | |
window.console && window.console.warn("Duplicate include, skipping:", url); | |
} else { | |
head.appendChild(element); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment