Chapter Two
Writing Your First JavaScript


Using JavaScript to Write Stuff on the Computer Screen

You are going to be able to program JavaScript before reaching the end of this chapter.

This is a devious and cunning move on my part to boost your self-esteem and confidence. (If I can do that, you may get the probably mistaken feeling that I actually know something about this stuff and can even help you to learn some things about it!)

After all, if you have actually accomplished things with JavaScript, you're going to feel way cool and powerful - almost like a real web wizard. You may even want to start shopping for a JavaScript Web Guru T-Shirt! (Like, if ya got it, ya oughta flaunt it!)

Finally: How to Display Stuff on Screen Using JavaScript!

Okay. You are now ready to write your own first JavaScript!

Well, almost. You need one more little bit of code: document.write();

This tells the Web Browser to display whatever appears inside the parentheses on the computer screen.

You may type the actual words you want (called a literal string) or you may put the name of a variable or variables you have defined within the parentheses. Or a combination of these. All work equally well.

You simply put the commands into your regular HTML like this:

<HTML>
<TITLE>Simple Write to Screen</TITLE>
<BODY BGCOLOR="white">
<CENTER>
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
// First write a literal to screen
document.write("This is a test!");
// Now set a string equal to a literal
var str="How now, Brown Cow?";
/* Write both a literal and a variable
   to screen */
document.write("<P>"+str);
// End hiding -->
</SCRIPT>
</BODY>
</HTML>

Click Here to run this script.

Just click the link above to see this script display in a new window on your computer. You'll also have a copy you can Cut-N-Paste to practice with yourself.

Notice that to combine literal text with a variable, you use a "plus" sign. (You could have also used a comma, but you'll be better off in the long run if you stick to the plus signs)

In the second write to screen, we used a literal HTML call (<P> for paragraph) so that this line would not be concatenated onto the end of the first test line.



© Copyright 1997, John H. Keyes john.keyes@intellink.net