views:

123

answers:

3

I get this error in FireBug when I try to access some CSS files hosted on external domains:

Security error" code: "1000
rules = styleSheets[i].cssRules;

The code I am using is:

$(document).ready(function () {
    $("p").live('mousedown', function getCSSRules(element) {
        element = $(this);
        var styleSheets = document.styleSheets;
        var matchedRules = [],
            rules, rule;
        for (var i = 0; i < styleSheets.length; i++) {
            rules = styleSheets[i].cssRules;
            for (var j = 0; j < rules.length; j++) {
                rule = rules[j];
                if (element.is(rule.selectorText)) {
                    matchedRules.push(rule.selectorText);
                }
            }
        }
        alert(matchedRules);
    });
});

Is there a way to fix this, beside moving all the css files on the same domain? Thank you

A: 

Sorry I do not think there is anything you can do about this as you can see from this forum post it looks like others are having a similar issues.

http://support.mozilla.com/tiki-view_forum_thread.php?comments_parentId=377420&amp;forumId=1

Can you give some more information as to what exactly you want to do with this information. Maybe there is another way to tackle your problem?

spinon
I am trying to get the clicked element class and CSS rules defined in the document CSS. The code works for same domain but it fails on cross domain. The firebug guys do something like this, use .cssRules to get that info even for cross domain. Probably they move the actual CSS file in a temp folder to remove that same origin error.
Mircea
So a user clicks an element and you want to get that class and the cssrules for that class? I will load something up later today and see what I can come up with. Seems like it should be possible since the CSS loaded into the page.
spinon
Yes, something like that. The only thing is that the CSS and JS files are on cross domains. The code above works for same domain. Thanx
Mircea
A: 

You may have to write a proxy for that CSS-file.

Peter Forss
A: 

If you have control over the domain where the external stylesheet is hosted, it may help to add an appropriate Access-Control-Allow-Origin header.

Access-Control-Allow-Origin: http://stylesheet-user.example.com
gerrit