# 2 Your First JavaScript Program Hiding JavaScript from Older Browsers
|
# 2 Review Questions | # 2 Assignment |
JavaScript programs are embedded in your HTML, usually in the <HEAD> section, but not always, and they run within the browser. Statements that form the actual program must be contained between the pair of <script>...</script> tags. Here is a sample JavaScript program. Click on the button below to see it run:
<SCRIPT> alert("I am an Alert Box. Aren't I cute?"); </SCRIPT> |
So, since you want your web sites to be awesome for most everyone, you want to specify "JavaScript" or "JavaScript1.1". We also want to make sure that the coded statements do not show up plain as day on someone's older browser. Since HTML ignores any tags it cannot understand, it may try to display the code in between the <SCRIPT> tags as part of the document- Ugh! So we fool it by hiding the code inside of comment tags, like this:
<SCRIPT> <!--This comment hides JavaScript from older browsers document.write("This is my first JavaScript program."); //end hiding --> </SCRIPT> |
In case you are wondering why the // in the next to last line, // is the symbol for a comment line in JavaScript. Commenting is very important, so that you, or someone else will be able to understand what is going on in a program, and maintain it for the future. Good comments can keep us from confusing ourselves.
To place a block of multiple lines of comments in a program, use the symbols /* ...as many JavaScript comment lines as you want ... */
Note that the first sample uses a pop-up alert box, and the second actually displays some text in a web page document. You have also seen two ways to add comments to a JavaScript program. Let's talk about JavaScript statements - the commands that add some action to your web page. Statements can be used to store information for later use, to display information, such as you saw in the demos, and much much more. Here are a few to get you started.
Note that they all end in a semi-colon; ; ; ; ; ; ; ; That is good JavaScript programming, although not always required. It is required in other programming languages such as C++ or Java, so it is a nice habit to get into..
Another important point is that JavaScript is a case-sensitive language. What that means is that alert() and Alert() and aLERt() are considered to be separate words. Most of the statements you will be using will be in lower case letters except for the text inside of "quotation marks". Anything in quotes will be showing up on the screen just as you type it, so be sure to use proper capitalization if you want your users to respect you and respect the contents of your web site.
<Center> <H2> <SCRIPT > document.write("The first line of output goes here."); document.writeln("This line will appear on the same line."); document.write("But this line will be on the next line down."); </SCRIPT> </CENTER></H2> |
These two statements will open a new document window, or browser window, and display the text that is between the quotes. Let's break these statements down.
<SCRIPT > alert("Hey - are you asleep at your computer?."); </SCRIPT> |
HELLO WORLD
It's a rite of passage. No matter what language people learn, their first program is usually one to output the message "Hello World" to the output screen. Well, what are you waiting for? Go to the link for Assignment 2 to walk through the steps to create the Hello World Program, and another one that is a little more useful.