/beyond/javascript/stips/article.php/3471461/HTML-Goodies-Script-Tip-Week-14.htm HTML Goodies: Script Tip: Week 14

HTML Goodies: Script Tip: Week 14

By Joe Burns

Hey, Tippo...

It's back to the big browser choice script and its catch-all last section. First off, if you need to refresh yourself on the script, here it is:

Click to See the Script

Last week we talked about the general concept of the script. It's saying that if the browser is this version and this type, then do this. But, at the very end of each of those little testing nuggets, is something that sets a strange variable, "r", to either one or two. Remember that? Take another look if you don't. If the browser is Navigator, then "r" is set to 1. An MSIE browser sets it to 2.

Why? It seems a bit silly to set something to 1 or 2 and then never bring it into play. If the browser is Netscape Navigator, then "r" is set to 1 and the script sends the user to a new page. Why even have that "r" variable if it is never going to be used?

Ah, but it isgoing to be used. What if your user is running an Opera, or Lynx, or Cello browser? Hmmmmm? Now we get into the catch-all at the end of the page. It looks like this:

if (r !=1 && r !=2){
//if browser is not Netscape or MSIE
//known versions go to text-based page.
//(caters for any other JavaScript Browsers).
parent.location.href="textpage.html" }

By turning the JavaScript to plain English, this is what the nugget is saying: "If "r" does not equal 1 and "r" does not equal 2, then go to this text-based page."

You see, the author never does care if "r" is given a value. In fact, "r" will only come into play if it never gets a value. That's why this is a catch-all. If the browser is none of the above, then send it to the text-based page. That's very clever.

So, now we get into a few more JavaScript operators:

  • An exclamation point (!) means "is not." This statement (r !=1) reads "r is not equal to 1."
  • The double ampersand (&&) means "and." Remember that you required a double equal sign to represent "equals"? Same thing here. You require double ampersand characters to mean "and." In case you're wondering, a single ampersand means "and bitwise." It's too much to get into here.

We'll spend one more Tip on this script and then get into a new one. Is all of this JavaScript stuff starting to make sense yet?

Next Week: Setting Multiple Variables as the Script Goes Along

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!

  • 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