Departments
 Info
   Home Page
   Articles
   Software Goodies

 Explanations
   How do they do that?
   The Web in Focus

 Resources
   Web Site Stuff
   Tech Books
   E-Books
   Tech Toys
   Web Hosting
   Links

 Women
   Opinion
   Tech Women
   Women's Studies

 Tech News
   Computer Security
   Databases
   Java
   Linux
   MP3
   PC Software
   Robotics
   Site Owner
   Tech Latest
   Web Development
   XML

 Spread the Word
   Newsletter
   Recommend this Page

 Site Info
   Legal Disclaimer
   Privacy
   Contact

 Lighter Side
   Crazy for Life
   crazy for romance
   Crazy for Kitties
   Crazy for Dogs
   Crazy for Cows

Copyright 2000-2001, hertechnology.com

 
The Web in Focus
Your Third Program

Checking values

In an if statement we test for whether something is equal by saying ==. If we want to see if something is NOT equal we use !=. These are just the rules of JavaScript. So, we can say "if indexOf returns a value of -1" like this:

if (navigator.appName.indexOf("Netscape") == -1)

We say "if index of does not return a value of -1" like this:

if (navigator.appName.indexOf("Netscape") != -1)

If again

If the value returned by indexOf is -1, we know the "Netscape" is NOT in the browser name. (That is Netscape with a capital N). But we don't know for sure that the browser name is Internet Explorer. The user could have something else. So we need to do another test, to see if "Internet Explorer" is in the browser name.

if (navigator.appName.indexOf("Internet Explorer") == -1)

If we get a -1 from indexOf in this if test, we know the name doesn't have Internet Explorer in it.

We can also write these if tests using NOT equals, !=, which will actually be a little easier. If indexOf does NOT return -1, that means the value IS in the browser name. So

if (navigator.appName.indexOf("Netscape") != -1)

means the browser is Netscape. And

if (navigator.appName.indexOf("Internet Explorer") != -1)

means the browser is Internet Explorer.

Else

If you recall from the second program discussion, an if statement gives you a way to tell the computer what to do when the if is false. The stuff to do when the if is false is signalled with the term else.

if (navigator.appName.indexOf("Netscape") != -1)
    document.writeln("Hey Netscape user!");
else
    document.writeln("You're not using Netscape");

In the above example, we'll print "Hey Netscape user!" to the screen if the browser name contains the value "Netscape", otherwise (else) we'll print out "You're not using Netscape". You can string along several tests together by saying else if. And you can specify one last thing to do if all of your previous if tests were false with a plain else.

if (navigator.appName.indexOf("Netscape") != -1)
    document.writeln("Hey Netscape user!");
else if (navigator.appName.indexOf("Internet Explorer") != -1)
    document.writeln("Hey IE user!");
else
    document.writeln("You're not using Netscape or IE");

Page 3

Tech Books

Tech Toys

More News

  Want to bookmark this site? Press Control-D. (Hold down the control key and the letter D at the same time.)