SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 13

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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!

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.