CSSRules is empty

Posted by Stephanie on Stack Overflow See other posts from Stack Overflow or by Stephanie
Published on 2010-06-02T20:59:00Z Indexed on 2010/06/02 21:04 UTC
Read the original article Hit count: 221

Filed under:
|
|

I have this very simple HTML page, and I'm trying to get the CSSRules of #poulet, but when I'm accessing the documents.styleSheets[0].cssRules I get this error in Chrome v5.0.375.55:
Uncaught TypeError: Cannot read property 'length' of null

Here is what my code looks like:

//HTML FILE
window.onload = function(){
var test = findKeyframesRule('poulet');
alert(test);
}

<div id="poulet">
allo
</div>

//JS FILE
function findKeyframesRule(rule)
{
    var ss = document.styleSheets;

for (var i = 0; i < ss.length; ++i)
{       
    for (var j = 0; j < ss[i].cssRules.length; ++j)
    {
        if (ss[i].cssRules[j].type == window.CSSRule.WEBKIT_KEYFRAMES_RULE && ss[i].cssRules[j].name == rule)
            return ss[i].cssRules[j];
    }
}
return null;
}

//CSS FILE
html, body{
background: #cccccc;
}

#poulet{
    border: 10px solid pink;
}

The files can be found here. I really need help on this one, please!!! D:

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html