0

I'm trying to dynamically load a stylesheet, and check whenever it's ready with cssRules property, but it's coming up as null, and I can't see why. (Chrome)

Here is an example:

http://jsfiddle.net/PeSqR/

var href = "http://twitter.github.io/bootstrap/assets/css/bootstrap.css";

var link = document.createElement('link');
link.setAttribute('href', href);
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');

var intervalId = setInterval(
    function(){
        try{
            //console.log(link.sheet);
            //console.log(link.sheet.cssRules);
            if(link.sheet && link.sheet.cssRules.length){
                clearInterval(intervalId);
                clearTimeout(timeoutId);
            }
        }
        catch(e){
            //console.log('err...' + e.message);
        }
    },
10);

var timeoutId = setTimeout(
    function(){
        clearInterval(intervalId);
        clearTimeout(timeoutId);
        alert('ERROR');
    },
5000);

document.getElementsByTagName('head')[0].appendChild(link);

Thanks for your help.

4

1 に答える 1