Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to get the content of a <noscript> tag using Javascript. I succesfully managed to get it in FF, Chrome, Opera and even IE6 but fail on IE7 (haven't tried IE8+ yet).

Basically, here's the reduced code version :

<noscript>Lorem ipsum</noscript> <script>     var noscript = document.getElementsByTagName('noscript')[0];     noscript.textContent; // undefined     noscript.innerHTML; // empty string     noscript.childNodes.length; // 0 </script> 

I tried adding element inside and targeting them, no success. I tried to wrap in a parent element and getting its .innerHTML, but anything between <noscript> tags is discarded.

Note : I'm building a lazyloader script and the <noscript> element is just what I need (<img> src attributes inside a <noscript> tag are not fetched by the browser.)

like image 572
pixelastic Avatar asked Nov 26 '10 01:11

pixelastic


People also ask

How do I check noscript tags?

To test the noscript tags, you can use an HTML validator to ensure you've placed the <noscript> tags in valid / legal places in your HTML. Other than that, just view your pages with JavaScript disabled to see the effect. Thanks - so the iframe stuff should be taken out of the noscript tag, correct?

What does noscript do in JavaScript?

Definition and Usage The <noscript> tag defines an alternate content to be displayed to users that have disabled scripts in their browser or have a browser that doesn't support script.

When noscript tag is executed?

Usage of NoScript tag: The tag defines alternate content that will be displayed if the user has disabled script or browser does not support script. It can be used inside both <head> and <body> tag.


1 Answers

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> 
like image 98
Tim Down Avatar answered Sep 19 '22 19:09

Tim Down