Created
December 7, 2015 22:16
-
-
Save notiv-nt/b07d971e9ded137ae642 to your computer and use it in GitHub Desktop.
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(arr, cache) { | |
var wl = window.localStorage; | |
var insert = function(fileContent, elem) { | |
var el = document.createElement(elem); | |
el.innerHTML = fileContent; | |
document.querySelector('head').appendChild(el); | |
}; | |
var _downloadFile = function(path) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', path, false); | |
xhr.send(null); | |
if (xhr.status == 200) { | |
return xhr.responseText | |
} | |
} | |
arr.forEach(function(obj) { | |
var data = wl.getItem(obj.path); | |
if (!data) { | |
data = _downloadFile(obj.path); | |
if (cache) | |
wl.setItem(obj.path, data); | |
} | |
insert(data, obj.tag); | |
}) | |
})([ | |
{ tag: "style", path: "css/main.css" }, | |
{ tag: "script", path: "js/main.js" } | |
], false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment