Skip to content

Instantly share code, notes, and snippets.

@noisan
Created October 2, 2015 07:27
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 noisan/f3b9738c02fc62fe5a08 to your computer and use it in GitHub Desktop.
Save noisan/f3b9738c02fc62fe5a08 to your computer and use it in GitHub Desktop.
Async CSS loader snippet inspired by https://github.com/filamentgroup/loadCSS/
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>loadCSS test</title>
<script>
(function(w, href) {
"use strict";
var d = w.document;
var ss = d.createElement('link');
ss.rel = "stylesheet";
ss.href = href;
ss.media = "only x";
/* ignore window.document.styleSheets, use link.onload() again */
ss.onload = function() {
ss.onload = function() {};
ss.media = "all";
};
/* use window.onload(), too... */
if (w.addEventListener) {
w.addEventListener('load', ss.onload);
} else if (w.attachEvent) {
w.attachEvent("onload", ss.onload);
} else {
var c = w.onload;
var b = ss.onload;
w.onload = function() { b.call(this); c&&c.call(this); };
}
var refs = (d.body || d.getElementsByTagName("head")[0]).childNodes;
var ref = refs[refs.length - 1];
ref.parentNode.insertBefore(ss, ref.nextSibling);
return ss;
}(this, './css.php?t=3'));
</script>
</head>
<body>
<p>This is a test page.</p>
<p id="test">test:</p>
</body>
</html>
<?php
header('content-type: text/css');
if (isset($_REQUEST['t'])) {
sleep($_REQUEST['t']);
}
?>
#test { background-color: greenyellow; }
#test::after{ content: "(OK!)"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment