5

According this site externalcss3 The CSS Rule object of the styleSheet object lets you access the individual rules of a stylesheet.

So If I try to run this piece of code(*) on stackoverflow page, using the javascript console, I expect to see some CSS Rules written in this page http://cdn.sstatic.net/stackoverflow/all.css?v=04e0337352b3.

(*)

var mysheet=document.styleSheets[0]
var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules
for (i=0; i<myrules.length; i++) {
     console.log(myrules[i].selectorText.toLowerCase());
}

Actually the result of console.log is something different from what I would expect:

object[type="application/x-shockwave-flash"], object[type="application/futuresplash"], object[data*=".swf"], object[src*=".swf"], embed[type="application/x-shockwave-flash"], embed[type="application/futuresplash"], embed[src*=".swf"]

What have I Missed? Sorry for my ignorance and your time.

3
  • 1
    I actually find that there are no rules in the only stylesheet (0). Still weird. Commented May 17, 2012 at 18:16
  • 1
    (Or a security error in Firefox - even stranger!) Commented May 17, 2012 at 18:21
  • I bet the stylesheet you are seeing is being injected by a browser extension... something like adblock, maybe (except adblock puts the stylesheet at the end, so must be something else). Commented May 17, 2012 at 19:11

1 Answer 1

5

It seems you can only access the rules in the CSS file if they are from the same domain.

Read this thread: Reading the rules of a cross domain CSS file in DOM

Sign up to request clarification or add additional context in comments.

2 Comments

Definitely correct, although I am curious why the OP could read any rules at all.
@apsillers - I have no idea. I correctly get a TypeError: Cannot read property 'length' of null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.