views:

1042

answers:

6

I'm looking into tightening up our ad code by moving it to an external jQuery script, but I obviously still need some HTML to target the ad to. So I was wondering if I can target a noscript element (or within a noscript element) since I'm going to have to leave that on the page anyway, or if I need to have some other element for the JavaScript to target?

<noscript>
  <div class="ad"><a href="foo"><img src="bar" alt="ad" /></a></div>
</noscript>

My intention would be to change or strip the noscript element.

+1  A: 

If the browser has javascript enabled, the content inside the no-script element is NOT displayed.

You would need to put it outside of that for the javascript targeting, and use the NoScript section for what its purpose is, users without JavaScript enabled.

Mitchel Sellers
I understand the reason for noscript, and I wasn't looking for whether or why it was displayed, but whether it was available to the DOM. When viewing my rendered source using Web Developer, I see the element is still there, thus my question as to whether it was available to target.
Steve Perks
I understand, but using it as a source for content, or a target for updated content with JS, is not a best practice, or even a suggested one.
Mitchel Sellers
A: 

You could always use the JS to hide that ad using CSS, if nothing else.

zigdon
+2  A: 

Why do you need the noscript element? If you are just going to remove or replace it with different elements when the javascript runs, then the div is acting as like a noscript element would anyway.

Phil
the noscript will display if there's no support for JS, which I have to consider. So, the noscript will always be there. If there is support for JS however, I could have used the content.
Steve Perks
Exactly. So just leave out the no-script tag and if there is Javascript do something with it rather than the other way around.
kouPhax
exactly my point, thanks kouPhax
Phil
That makes sense now, but the problem here is that we actually deliver different ad information if JavaScript is available, and presenting the content within noscript to the browser will result in pulling two ads rather than one. We'd get called out for doubling up our ad impressions.
Steve Perks
A: 

Most browsers will not let you target the noscript element, even if you (incorrectly) give it an id. You might try something like http://dev.opera.com/articles/view/replacing-noscript-with-accessible-un/ - it is a standard compliant method for achieving the same end, but without a noscript tag.

Lokkju
+3  A: 

<noscript> content is not only not displayed when JS is active, it apparently is also not in the DOM. I tried accessing content inside a <noscript> area (hoping you could clone() it with jQuery and insert it somewhere else) but got back nothing.

_Lasar
Thanks _Laser, that's the type of answer I was looking for.
Steve Perks
It is in the DOM. Using plain old nextSibling calls you will get the noscript section. My guess is that like Comment tags jQuery simply ignores noscript tags.
kouPhax
So you could technically traverse the DOM on page load using some nice non-jquery recursive calls and find the no-script tag and extract it's contents then do what you want with jQuery. I have done something very similar with Comment based annotations in Javascript.
kouPhax
@kouPhax: so are you saying that _Lasar is actually incorrect and noscript IS valid and available for the DOM?
Steve Perks
Yes, see my suggested answer.
kouPhax
I was only half wrong :) I only tested this in Safari, which apparently removes everything inside noscript tags from the DOM (the tags themselves appear to remain). jQuery consistently ignores noscript content, can't say why.
_Lasar
I tried this a while back with the same result. It could be (would have to check with jQuery devs) that jQuery ignores it precisely because it's *not* available in all browsers.
Andrew Hedges
+3  A: 

You can target noscript elemements.

    <noscript>asdfasFSD</noscript>

    <script>
     alert(document.getElementsByTagName("noscript")[0].innerHTML);
    </script>

This works in FF3, IE6 and Google Chrome. It will alert asdfasFSD for me.

kouPhax
Sadly not in Safari :/
_Lasar
Right, but I would be willing to bet (admittedly without any testing) that if you traverse the entire DOM using nextSibling you would be able to find the noscript tag.
kouPhax