Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disabled = true can be set dynamically on <style>. Why not statically?

To disable <style> blocks, all browsers allow setting document.styleSheets[x].disabled = true. However, only IE allows this property to be set on the tag itself, <style disabled="true">. Is there a workaround for this in other browsers? It seems odd that something done dynamically can't also be done statically.

like image 663
Lucent Avatar asked Apr 22 '11 15:04

Lucent


2 Answers

The style element has no valid attribute named disabled. From the HTML spec:

<!ELEMENT STYLE - - %StyleSheet        -- style info -->
<!ATTLIST STYLE
  %i18n;                               -- lang, dir, for use with title --
  type        %ContentType;  #REQUIRED -- content type of style language --
  media       %MediaDesc;    #IMPLIED  -- designed for use with these media --
  title       %Text;         #IMPLIED  -- advisory title --
  >

However, the HTMLStyleElement DOM interface does have such a property. From the DOM spec:

interface HTMLStyleElement : HTMLElement {
           attribute boolean         disabled;
           attribute DOMString       media;
           attribute DOMString       type;
};

Don't confuse an HTML element with its counterpart in the DOM. It is not "odd that something done dynamically can't also be done statically." The HTML and DOM specs were created to solve different problems. HTML is a markup language. The DOM is a convention for representing and interacting with the objects in a document.

like image 74
Wayne Avatar answered Oct 18 '22 04:10

Wayne


Extending on the media answer by @lampyridae, you can negate a media selector using not. To disable a style tag statically, I use media="not all", which works for me in Chrome 79.

like image 39
Neal Fultz Avatar answered Oct 18 '22 03:10

Neal Fultz