In Code.org's App Lab editor, we recently started seeing this error in Chrome 64:
Uncaught DOMException: Failed to read the 'rules' property from 'CSSStyleSheet'
The error is thrown in this function designed to detect whether CSS media queries are being used by the browser, on the line that includes styleSheets[i].cssRules
.
/**
* IE9 throws an exception when trying to access the media field of a stylesheet
*/
export function browserSupportsCssMedia() {
var styleSheets = document.styleSheets;
for (var i = 0; i < styleSheets.length; i++) {
var rules = styleSheets[i].cssRules || styleSheets[i].rules;
try {
if (rules.length > 0) {
// see if we can access media
rules[0].media;
}
} catch (e) {
return false;
}
}
return true;
}
The problem has been seen on Windows, OSX, Ubuntu, and ChromeOS; on Chrome versions 64.0.3282.167 and 64.0.3282.186. However, we've also seen this problem not happen on exactly the same Chrome version and platform - and we don't seem to be able to reproduce the problem in an incognito window.
What is the root cause of this error?
Access-Control-Allow-Origin: *
, you'll need to add thecrossorigin
attribute to the<link>
tag. Unfortunately, that isn't supported in IE11, in which case you'll need to use a different method, such as requesting the CSS text manually with XHR. – Sancha