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.)
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?
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With