Friday, March 29, 2024

HTML Goodies: Script Tip: Week 13

Hi, Tipster…

Okay, you were nice enough to go through the smaller browser detect script with me, now let’s hit a larger and much more involved browser detect script. Both scripts do the same thing, they look at the browser the user is running and send the user to a specific page.

This larger script is better than the dinky one I wrote in two ways: First, it looks not only at the “navigator.appName”, but also at the navigator version number. Second, there is a catch-all at the end that sends the browser to a page if it is none of the above. Mine didn’t have that. I just assumed if the browser wasn’t Netscape Navigator, then it must be Internet Explorer. This doesn’t assume. It has code that will send a third browser to a more text-based version of the page.


Click to See the Script


To start with, let’s just look at the general concept of the script. Click back and forth to follow along. The author has been nice enough to include a great deal of comments along the way which will make this much easier.

  • First, a variable “r” is created, but not given any actual value.
  • Second, the variable “browser” is created so you don’t have to keep writing navigator.appName again and again.
  • A variable for version is set up. In the first case it’s “4.0 (compatible; MSIE 4.0b1; Windows 95).”
  • Now the IF statement: If the browser is MSIE and the navigator.appVersion (the equal to navigator.appName except this one returns the version) is equal to the variable “version” we just set up, then…
  • Go to the parent.location.href msiepage.html.
  • And finally, if all this is true, say that “r” is equal to 2.

Next, there is the Else statement that will happen if the above If statement is not true.

But what if neither of them are true? Then the script keeps reading along. Notice the If statements keep coming. There are seven more.

But where are the other Else statements? If there’s an If, there has to be an Else, right? Wrong. There is an Else on the page, yes, but that’s just good JavaScript form. If you set up a series of If statements in which one of them is guaranteed to be correct, then why would you need the Else? One of the If statements will be correct. That’s what is happening here.

The only difficult part of this script is knowing what version numbers are out there. You already know that “Netscape” denotes the Navigator browser. Well, now you can see that “Microsoft Internet Explorer” denotes the MSIE browser. The easiest way to get the correct text for the navigator.appVersion is to look at the “About” menu item under Help on your browser. Luckily, this author was nice enough to find that format for us.

Read through the script. You’ll see exactly what’s happening along the way, until you get to the last block of testing script. It starts with this line: if (r !=1 && r !=2).

That’s the catch-all. It’s saying, “If it isn’t any of the above, then do this.” Notice it deals with that “r” variable I seem to have just glossed right over. I’ll explain what it means next time.

Next Week: That “r” Variable


Learn to write your own JavaScripts with the
JavaScript Goodies!


You can find many other uses for JavaScript
on your Web pages here!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured