title: Creating Style Node, adding innerHTML, add to DOM, and IE headaches
body: <p>I have a two part question. </p>
  
  <p>First, the scenario:</p>
  
  <p>Due to some bizarre issues we've run into in regards to mobile browser support for NOSCRIPT, I'm tasked with coming up with an alternative solution to 'detect' JS. The solution logic is to have two DIVs on the page. One is an error stating you do not have JS and his shown by default. If one has JS, we then want to add a new STYLE block to the HEAD that over-rides the previous CSS and hides the error and instead shows the content. </p>
  
  <p>The sample HTML:</p>
  
  <pre><code>&lt;div id="div1"&gt;div 1 (should be shown if JS enabled)&lt;/div&gt;
  &lt;div id="div2"&gt;div 2 (should be hidden if JS enabled)&lt;/div&gt;
  </code></pre>
  
  <p>This is the JS I started with:</p>
  
  <pre><code>  var styleNode = document.createElement('style');
    styleNode.setAttribute("type", "text/css");
    styleNode.innerHTML = "#div1 {display: block;} #div2 {display: none;}";
    headTag.appendChild(styleNode);
  </code></pre>
  
  <p>But, I was having problems. Some googling resulting in this description of a security issue that IE can have if you try to insert innerHTML into a created element before placing it in the DOM:</p>
  
  <p><a href="http://karma.nucleuscms.org/item/101" rel="nofollow">http://karma.nucleuscms.org/item/101</a></p>
  
  <p>So, I modified the script as such:</p>
  
  <pre><code>  var styleNode = document.createElement('style');
    styleNode.setAttribute("type", "text/css");
    var headTag = document.getElementsByTagName("head")[0];
    headTag.appendChild(styleNode);
    var aStyleTags = headTag.getElementsByTagName("style");
    var justAddedStyleTag = aStyleTags[aStyleTags.length-1];
    justAddedStyleTag.innerHTML = "#div1 {display: block;} #div2 {display: none;}";
  </code></pre>
  
  <p><em>question 1</em>: is that a valid workaround for the IE issue? Is there a more efficient solution?</p>
  
  <p><em>question 2</em>: even with the adjustment, the script still does not work in IE. It works fine in Firefox, but in IE 7 I get an "unknown runtime error".</p>
  
  <p>I have a sample of this code up on JSBIN:</p>
  
  <p><a href="http://jsbin.com/ucesi4/4" rel="nofollow">http://jsbin.com/ucesi4/4</a></p>
  
  <p>Anyone know what's going on with IE? </p>
  
  <p>UPDATE:</p>
  
  <p>I stumbled upon this link via google. Note the last comment:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx</a></p>
  
  <blockquote>
    <p>That said, you really should put all
    style rules in the HEAD for strict
    compliance with XHTML. Doing this can
    also be a little tricky because you
    cannot use innerHTML to inject into
    the HEAD or STYLE element directly.
    (Both of these tags are READ ONLY.)</p>
  </blockquote>
  
  <p>Eep! True? Is FireFox just being overly forgiving? Or is this just a very odd IE quirk?</p>
  
  <p>UPDATE 2:</p>
  
  <p>A bit more background on what we're trying to solve here. We're dealing with mobile devices and some of the antiquated devices a) don't support NOSCRIPT and b) have slow JS engines.</p>
  
  <p>Since they don't support NOSCRIPT, we are by default showing an error, then hiding it via JS if they have it, and presenting them with the proper content. Because of the slow JS engines on these, people see the 'flicker' of the DIV's showing/hiding. This was the proposed solution to handle that, as it would load the CSS before the DIVs were even rendered.</p>
  
  <p>Since it appears to be invalid, the solution will be that on these old devices, we'll use this method (as it seems to work, even if not in IE) and then all other proper browsers will do as suggested...we'll just update the DISPLAY CSS property via inline JS after each DIV is loaded in the DOM. </p>
  
  <p>All that said, I'm still curious as to whether this issue is an IE bug, or if IE is actually adhering to the proper standards by making STYLE a read-only element. </p>