views:

697

answers:

3
<noscript><div id="example">I want to get this innerHTML</div></noscript>

<script type="text/javascript"> alert($('example').innerHTML);</script>

This javascript snippet just returns an empty string. Is there a way of getting the contents of a noscript node?

p.s. I'm using prototype on this particular project.

+2  A: 

Testing with Firefox 3.0.7, Safari 3.2.2, and MSIE 7.0.5730.13 (all on WinXP SP3) it appears that everything within the <noscript> tags is completely omitted from the DOM tree.

It may be possible to access the <noscript> element itself, however, and then use DOM methods to change its child elements.

Alnitak
Didn't try the other two, but FF3.0.7 exposes the contents via textContent.
Shog9
+1  A: 

From the HTML 4.0 spec:

The NOSCRIPT element allows authors to provide alternate content when a script is not executed. The content of a NOSCRIPT element should only be rendered by a script-aware user agent in the following cases:

  • The user agent is configured not to evaluate scripts.
  • The user agent doesn't support a scripting language invoked by a SCRIPT element earlier in the document.

It seems to me that this implies that the entire contents of the NOSCRIPT tag (in this case, your div) are ignored altogether if scripting is enabled in the browser. Have you verified that the "example" div is accessible through the DOM at all in your case?

rmz
I've actually tested it (see my answer) - the div isn't in the DOM.
Alnitak
+2  A: 

No. Not reliably. You can sorta hack it in FF, IE, and Opera:

var nos = document.getElementsByTagName("noscript")[0]; 
// in some browsers, contents of noscript hang around in one form or another
var nosHtml = nos.textContent||nos.innerHTML; 

if ( nosHtml )
{
  var temp = document.createElement("div");
  temp.innerHTML = nosHtml;

  // lazy man's query library: add it, find it, remove it
  document.body.appendChild(temp);
  var ex = document.getElementById("example");
  document.body.removeChild(temp);

  alert(ex.innerHTML);
}

...But in Google's Chrome, the noscript element has no children in the DOM, and i would expect this to be true in other browsers as well as future versions of the browsers that i tested. You're better off finding some other place to store your data.

Shog9