<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.

share|improve this question

Try $($("noscript").text()) with jQuery. – sibidiba Aug 18 at 16:18
feedback

6 Answers

up vote 7 down vote accepted

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.

share|improve this answer
This does not seems to work on IE7 (XP). I does work on IE6, though. – Pixelastic Nov 25 '10 at 23:14
Try $($("noscript").text()) with jQuery. – sibidiba Aug 18 at 16:16
feedback

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?

share|improve this answer
I've actually tested it (see my answer) - the div isn't in the DOM. – Alnitak Mar 7 '09 at 0:02
feedback

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.

share|improve this answer
Didn't try the other two, but FF3.0.7 exposes the contents via textContent. – Shog9 Mar 7 '09 at 0:27
feedback

With a dirty fix its possible to make the content of the noscript tag available in all browsers. Read the solution here on my blog: http://blog.nolz.dk/?p=18

share|improve this answer
feedback

I'm not sure about prototype, but this works in Chrome with jQuery:

$('noscript').before($('noscript').text());
share|improve this answer
1  
I've used this successfully with IE9, FF9, and Chrome 16.0.912.77. Have to use a work-around for IE8 (and maybe earlier), in my case, to retrieve the content at the current URL via AJAX to insert into a jQuery UI tab as the actual tabbed content from a non-root url is served up in a noscript tag by default for a full request, only the tab content on an AJAX request. – tvanfosson Jan 26 at 22:37
feedback

Try $($("noscript").text()) with jQuery.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.