I load some stylesheets dynamically via JavaScript into my application. In this stylesheets I have different CSS variables that I want to read / write from my JS.
Stylesheets that are embeded directly in the head tag are handled correctly. The CSS variable gets evaluated by CSS and is visible in JS.
Stylesheets that are loaded dynamically during runtime do have some strange error. The CSS variable gets evaluated by CSS but is not visible by JS.
Anybody have an hint?
Plunker with minimal demo of error:
https://plnkr.co/edit/EChqjvZQJp7yz3L6nknU?p=preview
Method I use to load the CSS dynamically:
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "not_embedded_from_start.css");
document.getElementsByTagName("head")[0].appendChild(fileref);
Method I use to read the CSS variable:
var cS = window.getComputedStyle(document.querySelector("html"));
var pV = cS.getPropertyValue('--foo');
Example CSS:
:root {
--foo: green
}
#foo {
background-color: var(--foo);
}