Javascript Lesson 2

Goto Lesson:

Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Before you can write a script you must first be in Javascript mode. It is relatively simple to do that you just need to tell the browser what you are doing in HTML. Your Javascript commands would normally be in the head are of the page, but can be put elsewhere if being used to write text to the page. You would accomplish this like so:

<script language="Javascript">


And you are now in Javascript mode. You must make sure you tell the browser when you are done with the <b>/script</b> command. Now Javascript was around a long time ago but not originally so if someone happened to be using a really old browser (netscape and ie <2.0) the would get a big jumble of code. To prevent this we must have a way to tell old browsers to just ignore it all. To do this we will just put in an HTML comment line telling the browsers not to display it and the browsers that do read Javascript will ignore the HTML command in script mode. So now our code looks like this:

<script language="Javascript">
<!--Old browsers will ignore the following lines
</script>


We also must end this comment code though so that old browsers will read them once more. The problem with this is that Javascript will read this line in as and error so we must also tell Javascript browsers no to read that line with the Javascript comment command. For a one line Javascript comment (The line will be ignored by Javascript) we use double slashes (//). this makes the entire rest of the line a comment. If you want a multiple line comment you can use the /* comment and end it when your done with */ and everything in between will be ignored. So now our code looks like this:

<script language="Javascript">
<!--Old browsers will ignore the following lines
//javascript will ignore this line and lets turn html back on -->
</script>


Now that you know how to get into Javascript mode proceed to the next lesson to start scripting.