Using a Browser Sniffer
to Detect WebTV

June 8, 1999

A browser "sniffer" or detector is a useful tool for customizing your Web site to various categories of users. For example, you can use a browser sniffer script to deliver a certain Web page to your WebTV viewers and a different page to your Internet Explorer or Netscape visitors. You can create custom sniffers to detect all sorts of browsers; this article focuses on the detecting the WebTV browser with JavaScript and CGI.

JavaScript

The JavaScript code in this example uses the navigator object, which contains information about the software a viewer is using to view a Web page. Also, since browsers that do not support JavaScript are still being used, you should probably include a NOSCRIPT code block with a default BODY tag.

Once you have done the setup code, you can apply the if block anywhere in your HTML code where you would like to refine page delivery for a particular viewer.

JavaScript example:

<NOSCRIPT>
   <b>I see you've turned off JavaScript.</b>
</NOSCRIPT>

<SCRIPT LANGUAGE="JavaScript"><!--

if(navigator.appName.indexOf("WebTV") != -1) //WebTV detected
   document.writeln("How do you like that WebTV?");

else //Non-WebTV detected.
   document.writeln("When are you going to get WebTV?");

//-->
</SCRIPT>

 CGI

This CGI script separates WebTV from other browsers. The example noted below is a Perl CGI solution. This script detects the browser visiting the site by looking at its HTTP_USER_AGENT string. If that string contains "WebTV" then the browser is redirected to /tv_index.html. Otherwise, the browser is redirected to /ie_index.html.

Example of CGI script written in Perl:

#!/usr/local/bin/perl -w

$webTV_url = "/tv_index.html";
$other_url = "/ie_index.html";

if ($ENV{'HTTP_USER_AGENT'} =~ /WebTV/)
{
   $location = $webTV_url;
}

else # all others
{
   $location = $other_url;
}

print "Location: $location\n\n";