SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 15

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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) {

parent.location.href=”msiepage.html”
r=2


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
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. © 2026 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.