Cannot access cssRules from local css file in Chrome 64

here is a simple example of a problem:

<html>
<head>
<link rel='stylesheet' href='myStyle.css'>
<script>
window.onload=function(){
    try{
        alert(document.styleSheets[0]); // works
        alert(document.styleSheets[0].cssRules); // doesn't even print undefined
    }catch(e){alert(e)} // catch and alert the error
}
</script>
</head>
<body>
</body>
</html>

myStyle.css body{background-color:green;}

script works fine if you used <style></style>

SOLUTIONS:
1. works when files are on the network / localhost
2. works with other browsers (i.e. Internet Explorer, Microsoft Edge, Firefox)
3. chrome --allow-file-access-from-files

+4
source share
1 answer

TL DR: with Chrome 64, you will need to use a local development server to test functionality that depends on the CSS object model.

CSS , , Cross-Origin Resource Sharing (CORS), Chrome , , , .

Chrome 64.0.3282.0 ( 2018 , ) . , .

a4ebe08 Chromium:

CSSStyleSheet

Spec : https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface

: SecurityError, :

  • cssRules()/rules()
  • insertRule()
  • deleteRule()

: CORS CSS . W3C , CSS .

, , (/localhost), , . MDN ? - , , CORS .

, .

+7

Source: https://habr.com/ru/post/1693548/


All Articles