The Ugly JavaScript Book - Chapter Two

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.

Links: Introduction | Chapter One | Chapter Three | Chapter Four | Chapter Five | Chapter Six | Chapter Seven | Chapter Eight | Chapter Nine | Chapter Ten | Chapter Eleven | Chapter Twelve | Chapter Thirteen | Chapter Fourteen | Chapter Fifteen | Chapter Sixteen | Chapter Seventeen | Chapter Eighteen | Chapter Nineteen | Chapter Twenty | Chapter Twenty-one | Chapter Twenty-two | Chapter Twenty-three | Appendix A | Appendix B | Appendix C | Appendix D | Cover
This free virtual book provided courtesy of:
AutoScripter(tm), the site for professional Web Designers and Developers and
WebWalker Virtual Press, virtual publishing on the Web since 1994
Other free e-books from WebWalker: The All American Terrorists | The Gnomes of Jost | For the Love of Entropy | The Trees of Kratva | The Arch of Rain Magicians | Lightshow | The Black Mancutters Guild | The Septoracle

© Copyright 1997, 1999, 2000, 2001 John H. Keyes
editor@jorika.com