JavaScript Tutorial Part I- Some Basics
By Will Bontrager2004-02-09
How the browser knows it is JavaScript
JavaScript program lines must be between <script...> </script> tags unless the line is within an HTML tag. The JavaScript between the <script...> </script> tags must also be enclosed with HTML comment tags.
Often, you'll find JavaScript in this format:
<script language="JavaScript">
<!-- programmer comments can go here
JavaScript program code goes here
// programmer comments can go here -->
</script>
The language="JavaScript" part is not specifically required. However it is good to get in the habit of typing it in because there are other script languages out there, Active X for example, and the language designation tells the browser which scripting language is being used.
Without the <!-- and --> HTML comment begin/end tags, the browser tries to display the JavaScript as regular text -- this last statement may not be true for the very latest browsers, but it is true for most recent browsers and it certainly is true for browsers that are not JavaScript enabled.
The second line contains the HTML comment begin tag. The JavaScript interpreter ignores the entire line so long as it begins with that tag. Some people put a copyright notice or other comments on the rest of that line.
Notice the characters
//
on the second from last line. Two slashes next to each other tell the browser's JavaScript interpreter that the rest of the line is not valid JavaScript code -- it is ignored the same as all programmer comments are ignored by the interpreter. The rest of the line contains, of course, the HTML end comment tag. (You can use the space between // and --> for comments of your own -- the JavaScript interpreter will ignore it, and it is still inside the HTML comment tag so the HTML interpreter will ignore it, too.)
You can put your comments anywhere in a JavaScript program so long as they begin with the characters: //
There is no "end" comment code for JavaScript. All JavaScript comments that begin with // end automatically at the end of that same line.
Sometimes, you'll find JavaScript within HTML tags:
For mouseovers and some other functionality, you will find JavaScript code within the anchor tag. Example:
<a href="somedomain.com"
onMouseover="do_some_function()"
onMouseout="do_different_function()">
<img ...>
</a>
JavaScript code is also found within other HTML tags. Two of the most used are the <body...> and <input...> tags.
An example of JavaScript code in a <body...> tag:
<body onLoad="do_some_function()">
An example of JavaScript code in an <input...> tag:
<input type="submit" onClick="do_some_function()">
The reason non-JavaScript enabled browsers don't get confused when they find JavaScript in HTML tags is because they are programmed to ignore anything in tags that they do not recognize.
Tutorial Pages:
» JavaScript Tutorial Part I- Some Basics
» Orientation
» How the browser knows it is JavaScript
» This is how JavaScript works
» Some programming basics
» How to make your program remember things
» Here it is, all put together
» Using strings of characters
» To come
Copyright 2004 Bontrager Connection, LLC
