I know document.styleSheets which consists of all valid style sheets in a page. I want to know whether i can create a new one and append it to present list via javascript.
I have tried document.styleSheets[0].constructor, document.styleSheets[0].__proto__.constructor, new CSSStyleSheet, CSSStyleSheet(), all what i get from Chrome is TypeError: Illegal constructor. CSSStyleSheet.constructor() returned a pure object but i expect a CSSStyleSheet object.
I know i can create a link/style element and append it, then modify it. What i want to know is that, can i create such object directly with javascript?
As far as I know, the only approach that comes close to what you’re asking for is the IE-only
document.createStyleSheet([url] [,index])method which you can use to create up to 31*styleSheetobjects (after which you’ll still need to manually createstyleelements and append them todocument).This answer shows how you can define the
createStyleSheet()method for non-IE browsers but as you’d expect it does so by appendinglink/styleelements (which for some reason you’re trying to avoid).* IE 6 to 9 is limited to 31 imported stylesheets due to 5-bit field used for storing sheet IDs. In IE10 this limit has been raised to 4095.