Skip to content

Instantly share code, notes, and snippets.

@sandro-pasquali
Created September 13, 2013 14:59
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 sandro-pasquali/6551847 to your computer and use it in GitHub Desktop.
Save sandro-pasquali/6551847 to your computer and use it in GitHub Desktop.
Cross-browser CSS-loaded detector, without browser sniffing.
#targ {
position: absolute;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function loadCSS(src, cb) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.setAttribute('href', src);
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('charset', 'utf-8');
head.insertBefore(link, head.firstChild);
(function check() {
if(link[document.createStyleSheet ? 'styleSheet' : 'sheet']) {
return cb && cb(link);
}
setTimeout(check, 0);
})();
}
setTimeout(function() {
loadCSS('style.css', function(link) {
var el = document.getElementById("targ");
el.style.left = "100px";
});
}, 1000);
</script>
</head>
<body>
<div id="targ">left?</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment