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 Second Program

The second program

The program goes in an HTML file inside special <SCRIPT> tags. Create a file and either type or copy what you see on the screen into your file. You can name the file anything you like. My version is called focus3.html.

<html>
<head><title>Second Program</title>
</head>
<body>
<SCRIPT>
if (navigator.appName == "Netscape")
  document.writeln("Hey Netscape user!")
else
  document.writeln("Hey IE user!")
</SCRIPT>
</body>
</html>

I put the else term on its own line so it is easier to read. If you do type this in, be sure to type in the JavaScript exactly as you see it -- upper and lower case letters especially. HTML can be typed in upper or lower case, but JavaScript can't. Note that navigator.appName has a capital N in Name.

Try it. You may want to bring up your other browser and try it again to see if you get a different message. (You might not, as you'll see next.)

There is a flaw in this program that I'll fix at a later time by using a JavaScript function called indexOf. I'll talk about functions and show using indexOf to make this little program more robust.

The flaw is this. If the name of the browser as given by navigator.appName is something like "Netscape Navigator", the if test will give a false and the stuff you do when the if test is false will be done -- namely, the words "Hey IE user!" will be printed to the screen. This result is clearly wrong. The problem is that the if test expects the name of the browser to be exactly the word "Netscape" (without the double quotes). Even if the name is given as "netscape" with a lowercase n, the test will be false.

While checking for the word "Netscape" exactly works fine on my PC running Windows NT, doing the test like that is not guaranteed to work all the time. A better way would be to see if the word "Netscape" appeared anywhere in the name. That is what indexOf will do. (indexOf won't take care of the upper/lower case situation, but that can be taken care of, too, in another way.)

Another problem is that if you are running a browser other than Netscape or Internet Explorer, the program will say "Hey IE user!". This situation can be fixed by adding another test. I'll leave these improvements for next time.

So, now you've gotten through another program. Stay tuned for more!

Written June 30, 2000

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