Last active
August 29, 2015 14:10
-
-
Save nadavspi/642dff1826682e49c7bc 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() { | |
var files = ['assets/site/css/non-critical.css', 'assets/site/img/icons.svg']; | |
function getExtension(src) { | |
return src.split('.').pop(); | |
} | |
function injectContent(content, type) { | |
if (type === 'css') { | |
var style = document.createElement('style'); | |
document.head.appendChild(style); | |
if (style.styleSheet) { | |
style.styleSheet.cssText = content; | |
} else { | |
style.innerHTML = content; | |
} | |
} else if (type === 'svg') { | |
document.head.insertAdjacentHTML('beforeend', content); | |
} | |
} | |
function loadFile(file) { | |
var content = localStorage[file]; | |
if (content) { | |
injectContent(content, getExtension(file)); | |
} else { | |
window.addEventListener('load', function get() { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', file, true); | |
xhr.addEventListener('load', function() { | |
if (xhr.readyState === 4) { | |
injectContent(xhr.responseText, getExtension(file)); | |
localStorage[file] = xhr.responseText; | |
} | |
}); | |
xhr.send(); | |
window.removeEventListener('load', get); | |
}); | |
} | |
} | |
if (window.localStorage) { | |
files.forEach(function(file) { | |
if (getExtension(file) === 'svg' && !document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#Image', '1.1')) { | |
return; | |
} | |
loadFile(file); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment