Skip to content

Instantly share code, notes, and snippets.

@notiv-nt
Created December 7, 2015 22:16
Show Gist options
  • Save notiv-nt/b07d971e9ded137ae642 to your computer and use it in GitHub Desktop.
Save notiv-nt/b07d971e9ded137ae642 to your computer and use it in GitHub Desktop.
(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