Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing cross-domain style sheet with .cssRules

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?

like image 312
Mircea Avatar asked Jul 09 '10 09:07

Mircea


People also ask

How do I access an external style sheet?

External CSS With an external style sheet, you can change the look of an entire website by changing just one file! Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.

How do I link two style sheets in HTML?

Note: There are two different ways to import a CSS file into another using @import url(“style2. css”); or @import “style2. css”; or directly import any CSS file or multiple CSS file in the HTML file directly within <style>@import “style1.

Can you link two style sheets CSS?

Yes, you can include multiple style sheets, but you need to label them as alternate style sheets and give the user some way to activate them using JavaScript - perhaps by clicking a link.


1 Answers

The only real solution to this problem is to CORS load your CSS in the first place. By using a CORS XMLHttpRequest to load the CSS from an external domain, and then injecting the responseText (actually responseCSS in this case) into the page via something like:

function loadCSSCors(stylesheet_uri) {   var _xhr = global.XMLHttpRequest;   var has_cred = false;   try {has_cred = _xhr && ('withCredentials' in (new _xhr()));} catch(e) {}   if (!has_cred) {     console.error('CORS not supported');     return;   }   var xhr = new _xhr();   xhr.open('GET', stylesheet_uri);   xhr.onload = function() {     xhr.onload = xhr.onerror = null;     if (xhr.status < 200 || xhr.status >= 300) {       console.error('style failed to load: ' + stylesheet_uri);     } else {       var style_tag = document.createElement('style');       style_tag.appendChild(document.createTextNode(xhr.responseText));       document.head.appendChild(style_tag);     }   };   xhr.onerror = function() {       xhr.onload = xhr.onerror = null;       console.error('XHR CORS CSS fail:' + styleURI);   };   xhr.send(); } 

This way the CSS files will be interpreted by the browser as coming from the same origin domain as the main page response and now you will have access to the cssRules properties of your stylesheets.

like image 110
Jordan M Alperin Avatar answered Sep 28 '22 03:09

Jordan M Alperin