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

Have you ever wanted to write a computer program? Or are you curious about what programming is like? If you follow along with this column, you can join the ranks of the programming elite! Or, you may ask yourself how anyone can do this of their own free choice! Hopefully, either way you'll get taste of what programming is like.

Preparations

Before we start, you have to have the following:

  1. Patience. The computer is like your pickiest English teacher. Make the equivalent of a grammer or spelling mistake and the computer will "red-ink" your program with errors.
  2. Attention to detail. Now is not the time to take writer's creative liberties. Type in the JavaScript exactly as it is shown.
  3. A recent browser verion. Either Netscape Navigator (NN) or Internet Explorer (IE) is fine, but make sure you have one of the later 4+ versions. NN might be a better choice because it is easy to see any script errors in the Navigator console window. (Hopefully we won't make any of those!) If you don't have a recent (say in the last 18 months) browser, now is the time to go to whichever-browser- you-prefer's site and download it.
  4. JavaScript enabled in your browser. It's enabled by default, so if you haven't disabled it, you are ready to go.

JavaScript and HTML

JavaScript is a series of instructions that your browser understands and executes. The kinds of instructions that you can enter are restricted -- you have to follow the language and syntax of JavaScript (as decided upon by the designers of the language).

JavaScript code can be kept in a separate file, but it can also be in the file with the HTML tags. (HTML? HTML is the basic stuff that browsers process to put your web page on the screen. It consists of the text you want to display, plus tags that control how the text looks, what's a link, where the images are located, and more. We'll use some simple HTML to create a page for the JavaScript. If you don't know HTML, don't worry. You can copy and paste the code from the screen. All you need to know how to do is edit a file with a TEXT editor like Notepad.)

Here is the HTML document that we will start with. Note: you can type HTML tags in upper or lowercase. They are the same.

<html>
<head><title>First Program</title>
</head>
<body>
</body>
</html>

The code

By tradition, the first computer program someone writes in a new language prints "Hello World!" on the screen. (Some people like to have the first programs print something else, but, hey, I'm something of a traditionalist.)

JavaScript can be in the head of your document or in the body. Don't worry now why you might want to put something in one place or the other. We're going to put this first program in the body.

Also, to make the program a little more interesting, we'll have it show which browser you are running when you load the HTML page with the script.

Inside of HTML, JavaScript needs to be surrounded by <SCRIPT> tags, so we will tell the browser a script is coming with the opening <SCRIPT> tag and end it with the closing </SCRIPT>. Because some old browsers don't like scripts, we will put the script code inside of an HTML comment. Below you will see the script tags and the comment tags added to the basic HTML.

<html>
<head><title>First Program</title>
</head>
<body>
<SCRIPT>
<!--
//-->
</SCRIPT>
</body>
</html>

Now we want to print "Hello World!" (without the quotes) to the screen. JavaScript uses a command called document.writeln to do the printing to the screen. We put what we want to write inside of double quotes, inside of parenthesis, like so: document.writeln("Hello World!"); (remember, no creative liberties -- you must use the exact syntax with parenthesis and an opening and closing quote). Add that statement after the opening SCRIPT tag:

<html>
<head><title>First Program</title>
</head>
<body>
<SCRIPT>
document.writeln("Hello World!");
</SCRIPT>
</body>
</html>

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