Created
February 25, 2014 21:54
-
-
Save fmal/9218686 to your computer and use it in GitHub Desktop.
postload css
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() { | |
'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