Skip to content

Instantly share code, notes, and snippets.

@fmal fmal/loadcss
Created Feb 25, 2014

Embed
What would you like to do?
postload css
(function() {
'use strict';
function loadCssWithLink() {
var l = document.createElement('link');
l.rel = "stylesheet";
l.href = 'css/css.css';
var sc = document.getElementsByTagName('script')[0];
sc.parentNode.insertBefore(l, sc);
}
function loadCssWithAjax() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'css/css.css', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
loadCssWithLink();
}
}
};
xhr.timeout = 3000;
xhr.send();
}
var cutsTheMustard = function() {
if (
'querySelector' in document &&
'addEventListener' in window &&
'XMLHttpRequest' in window
) {
return true;
} else {
return false;
}
};
if (cutsTheMustard() === true) {
loadCssWithAjax();
} else if (cutsTheMustard() === false) {
loadCssWithLink();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.