Skip to content

Instantly share code, notes, and snippets.

@fmal
Created February 25, 2014 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmal/9218686 to your computer and use it in GitHub Desktop.
Save fmal/9218686 to your computer and use it in GitHub Desktop.
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