Adding a background-image rule via the CSSOM
suggest changeTo add a background-image rule via the CSSOM, first get a reference to the rules of the first stylesheet:
var rules = document.styleSheets[0].cssRules;
Then take the index just after the last rule. insertRule inserts before the given index, so the index must be rules.length to append:
var end = rules.length;
Finally, insert a background-image rule for the body element at the end of the stylesheet:
rules.insertRule("body { background-image: url('http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico'); }", end);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents