3
I’m getting the style sheet rules for this situation addressed in this question.
But if you use internal CSS (with tag <style>
) I correctly get the declared styles, as you can see in the following example:
var sheets = document.styleSheets;
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (rules[r].selectorText) {
// para imprimir no DOM
document.body.appendChild(document.createTextNode(rules[r].cssText));
document.body.appendChild(document.createElement("br"));
}
}
}
p {
color: red;
}
h1 {
font-size: 16pt;
}
I already use an external CSS file, the document.styleSheets
even brings the style sheet, but the rules (rules
or cssRules
) are null
, and the attribute href
is with the value of url
external pointing to the file. As can be seen in the following example:
var sheets = document.styleSheets;
for (var i in sheets) {
if (sheets[i] instanceof CSSStyleSheet) {
console.log(sheets[i]);
}
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (rules[r].selectorText) {
// para imprimir no DOM
document.body.appendChild(document.createTextNode(rules[r].cssText));
document.body.appendChild(document.createElement("br"));
}
}
}
<!-- Utilizei o CSS do bootstrap por ser um CSS confiável externo para demonstrar meu problema -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.css">
<p>Verifique o console para observar as saídas!</p>
Question
Is there any way to get CSS rules using external CSS file (Example 2), just like with internal CSS (Example 1)?
Testing here I noticed that in Firefox, this is working, but in Chrome, IE10 and Opera not.
Are you not trying to get the rules before the CSS is finished loading and parsing? Try running this on
window.onload
.– bfavaretto
@bfavaretto, I believe this is not the problem, because I am running it from a button, and at that moment the page is already stylized, so the CSS is already loaded.
– Fernando Leal
I suspect this is due to a permission issue when accessing a style sheet on another domain. A MDN goes so far as to say that this can make an exception (which I have not seen happen in Chrome). You have tested with a CSS in the same domain as your application?
– bfavaretto
@bfavaretto, to tell you the truth I’m just testing the local application, I have no knowledge whether this is considered same domain or not?
– Fernando Leal
Yes, as long as it is referred to the same way in the browser URL and CSS (e.g., "localhost" or "127.0.0.1" on both), and that you are not loading the page with
file:///
.– bfavaretto
@bfavaretto, I will install an application server here because I am running on
file:///
, like you said. =(– Fernando Leal
@bfavaretto, you were right this seems to be a different domain loaded style/CSS problem as quoted in the reference you quoted from MDN, where testing here on an application server, on
localhost
or127.0.0.1
. It seems that this works as expected. Thanks for the tip. And if you think it is feasible to create an answer, it would be interesting for further research.– Fernando Leal