views:

365

answers:

5

This is probably a simple question, and I'm slightly embarrassed to ask it, but I've been working with this chunk of JavaScript ad code for a while and it's bothered me that it's never really made sense to me and is probably out dated now with modern browsers. My question is, do we need to check for browser types still, and what is that second bit of script doing?

<script type="text/javascript">
  document.write('<scr' + 'ipt src="" type="text/javascript"></scr' + 'ipt>');
</script>
<script type="text/javascript">
  if ((!document.images && navigator.userAgent.indexOf('Mozilla/2.') >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
    document.write('<a href="">');
    document.write('<img src="" border="0" alt="" /></a>');
  }
</script>

I'd like to clarify that I'm actually calling someone some ad code, so while I could check for browser types, that would really be the responsibility of the keeper of the code. I'd love it if I could get this into jQuery - but I'm having trouble with the call (see my other post below).

What I was wondering is, do I still need to check for these browser types?

Cheers,
Steve

+4  A: 

Mostly we use javascript libraries like jQuery which handle this kind of thing for us.

Strangely I find myself hacking per-browser CSS much more often these days.

Ali A
I understand that jQuery handles this, but I'm actually trying to build a call to someone elses script that isn't built with jQuery.
Steve Perks
+2  A: 

It's better to check whether the browser DOM supports specific features rather than dealing with the user agent string directly.

John Topley
+1  A: 

On the second snippet of code: it's checking for two things:

  • That the browser opening the document supports the document.images portion of the DOM, that the document contains any images, and the browser's UserAgent string (an identifier) contains "Mozilla/2.",
  • OR that the UserAgent string contains "WebTV"

in those cases, it outputs an empty link and image tag.

Adriano Varoli Piazza
A: 

This is why APIs (such as jQuery) are used. They offload the act of checking browser compatibility (and future compatibility) away from the developer. When a new browser or new version of a browser comes along, the API is updated, if necessary, so that your code continues to work.

So see if you can come up with a solution with jQuery, and you won't have to worry about this. For future compatibility, you might have to get the latest version of jQuery, but the theory is that your code won't have to be updated.

Moskie
A: 

It's nice to hear that so many people are championing jQuery, but the code I'm adding is targeting an external script that I have no control over and my issue is that whenever I call this script using jQuery, the ads open up on their own page:

$(this).append().html('<script src="http://ad.doubleclick.net/adj/' + site + '.iclick.com/adtarget;subss=' + subss + ';subs=' + subs + ';area=' + area + ';site=' + site + ';kw=' + kw + ';sz=' + $adSize + ';pos=' + count + ';tile=' + tilecount + ';ord=' + zzzzadslotzzzz + '"></script>');

I'd love to move this stuff into jQuery (and that's my goal, as I'm wanting to stop the ads from loading until the document's ready)

Thanks,
Steve

Steve Perks