How to get content of <noscript> in Javascript in IE7? How to get content of <noscript> in Javascript in IE7? javascript javascript

How to get content of <noscript> in Javascript in IE7?


In IE 7 and 8, it's simply impossible to retrieve the contents of a <noscript> element. Any content between the <noscript> and </noscript> tags in the HTML is not reflected in the DOM in IE, the element has no children and innerHTML and innerText are empty strings.

In IE 6, the situation is curious: in common with IE 7, the <noscript> element has no child nodes but its contents are reflected in the innerHTML and outerHTML (but not innerText) properties of the element.

All this being the case, your only option in IE is to put the content in your <noscript> element inside some other element instead. To emulate the behaviour of a <noscript> element, you could put the content in an element that is immediately hidden by JavaScript (when script is enabled):

<div id="noscript">Lorem ipsum</div><script type="text/javascript">    document.getElementById("noscript").style.display = "none";</script>


Not the most elegant solution - Images load as normal with IE7 and IE8, and all other browsers get the added benefit of lazi loading. You will end up with some empty comment blocks in the final output... but who cares, right?

<!--[if (gt IE 8)|!(IE)]><!--><noscript><!--<![endif]-->  <img src="/path/to/img.jpg" alt="photo" /><!--[if (gt IE 8)|!(IE)]><!--></noscript><!--<![endif]-->

JavaScript (jQuery - if you need real JS let me know):

jQuery(function($) {  $('noscript').each(function() {    var $this = $(this);    $this.replaceWith($this.text());  });});

Tested with IE7+, Chrome, FF3.6+, Opera11


One work around for this is to duplicate the content of noscript as its attribute.

For example:

<noscript id="ns" alt="Lorem ipsulum">Lurem ipsulum</noscript>

On the script get the value of alt attribute instead of its innerHTML

<script>   var ns = document.getElementByid('ns');   var htm = ns.innerHTML || ns.getAttribute('alt');   alert(htm);</script>