HTML Goodies: Script Tip: Week 15

By Joe Burns

Hi, Tip...

We'll take one more look at the big browser detect script. This time around we'll discuss the author setting up variables as the script goes along. Take a quick look at the script again:

Click to see the Script

Up until now, the scripts you've seen have all had the variables set up front. This script's a little different in that the author is only setting the variables when he needs them. It saves a little time along the way. Some feel it's bad script writing in that it appears as if the script wasn't fully thought out. But all that aside, it sure saves wear on the fingers.

Let's look at the first little block of script:

version="4.0 (compatible; MSIE 4.0b1; Windows 95)"

if (browser == "Microsoft Internet Explorer" && navigator.appVersion==version) {


Now, let's read it. First the author sets a variable just for this little block of script. He writes "version="4.0 (compatible: MSIE4.0b1; Windows 95)." See that above?

Did you think that you had to always write "var" in front to make something a variable? I did, too, for a long time. You don't. The single equal sign does the trick. In my scripts I still do it, though. It helps me read it all later.

The format of the version number came from the browser itself. If you look at the HELP, ABOUT section you'll get the text the browser uses to describe itself. So, now the author has set a variable for the version. Moving on...

The If statement reads "If the browser (remember the overall variable "browser" was set at the top of the script -- look again if you have to) is equal (double equal signs) to Microsoft Internet Explorer and (double &) the navigator.appVersion (object representing the browser version) is equal to (double equal signs) version (the variable set up just above), then go to msiepage.html.

JavaScript is so wonderfully linear. It reads a lot like a speaking language. So, what happens in each little blip of script:

  • A variable is created representing a browser version.
  • The script checks the browser for type
  • If the type does not match, the script moves along and checks with the next blip of script.
  • If the type does match, then the browser checks the version against the version variable set up at the beginning of the blip of script.
  • No match? Move along.
  • Match? Then go to the page denoted in the next line.

Follow along in the script. You'll see the same process of creating a variable then checking the browser version against it. It's a pretty clever way of doing things. You'll need something to test the browser again, so why not create it?

So, there you go. You've rolled through another script. We start at the beginning again with a new one next week.

Next Week: New Script! Random Number Generator

Learn to write your own JavaScripts with the
HTML Goodies 30-Step Primer Series
and take the knowledge home with you in
JavaScript Goodies!

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

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date