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

This time the focus is on programming again. If you've never written any sort of program, take a minute to read Your First Program.

In the first programming article, I mentioned a few points about programming in general: patience and attention to detail. These points are still super important -- you just can't program without them!

From the first program

The first program introduced an important aspect of a program -- statements. A statement is similar to a sentence in English; it is a complete unit in the programming language that accomplishes something.

If tests

The second program will also be in JavaScript inside an HTML file and will make use of statements similar to those used in the first program. The second program will also make use of a conditional -- an "if" statement.

You use if statements in regular conversation frequently: "If it's sunny tomorrow, I'm going to work in the garden, otherwise, I'll catch up on paperwork inside". That statement has several parts.

  1. The if clause: "if it's sunny tomorrow". This can also be called an "if test".
  2. The action you are going to take if it is sunny: "I'm going to work in the garden". This is the stuff you do when the if test (condition) is true.
  3. The action you are going to take if it is NOT sunny: "I'll catch up on paperwork inside". This is what you'll do when the if test (condition) is false.

A programming sort of thing to say might be: "If the browser is Netscape, print out 'Hey Netscape user!', otherwise, print out 'Hey IE user!'". In JavaScript, you can say something similar, but you have to use the particular words and syntax that JavaScript wants to see.

  1. "If the browser is Netscape" becomes
    if (navigator.appName == "Netscape")
  2. "print out 'Hey Netscape user!'" becomes
    document.writeln("Hey Netscape user!")
  3. "otherwise, print out 'Hey IE user!'" becomes
    else document.writeln("Hey IE user!");

If you look closely, you'll see I added some ()'s and a ; to the text. The ; is optional, but the ()'s are necessary. They tell Javascript what the if test is. Inside of the ()'s, you will see a double equals sign ==. That means to compare two things and see if they are the same or not. The term else identifies the stuff to do when the if test is false. It means "otherwise" in English.

The first program used the document.writeln command to print a message to the screen. It does the same thing here. The first program also used navigator.appName -- this little gizmo gives you the name of the browser you are running when you load the HTML page with the JavaScript.

The if test takes name of the browser and compares it to the word Netscape. If the name is exactly Netscape, the if test is true, otherwise the if test is false.

Page 2

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