JavaScript Basics :
THE FIRST SCRIPT








Knowing that javascript needs to be entered between <script> tags, is a start. But there are a few other things you need to know before writing your first javascript:
  • Javascript lines end with a semicolon.
    You may have noticed from the example on the previous page that javascript lines end with a semicolon.
    You can easily put all your javascript on a single line without destroying the performance of it.
    However, you would destroy the overview of your script so it is not advisable.


  • Always put the text within " ".
    When entering text to be handled by javascript, you should always put the text within " ".
    If you forget to enclose your text in " ", javascript will interpret your text as being variables rather than text.
    In the next section you will learn why this would cause an error to your script.


  • Capital letters are different from lowercase letters.
    You should always remember that capital letters are different from lowercase letters.
    This means that when you write commands in javascript, you need to type capital letters in the correct places, and nowhere else.


Incorrect capitalization is probably the most common source of error for javascript programmers on all levels!!




Now consider this example:

Instead of having javascript write something in a popup box we could have it write directly into the document.

<html>
<head>
<title>My Javascript Page</title>
</head>

<body>
<script>
document.write("Welcome to my world!!!");
</script>

</body>
</html>


The document.write is a javascript command telling the browser that what follows within the parentheses is to be written into the document.

Note: When entering text in javascript you need to include it in " ".

The script in the example would produce this output on
your page:

Welcome to my world!!!






Consider this example to learn where javascript writes the text:

<html>
<head>
<title>My Javascript Page</title>
</head>

<body>
Hello!!!<br>
<script>
document.write("Welcome to my world!!!<br>");
</script>

Enjoy your stay...<br>
</body>
</html>


The output from this example would look like this:

Hello!!!
Welcome to my world!!!
Enjoy your stay...


As you can see, javascript simply writes the text to where the script is placed within the HTML codes.

An interesting aspect is that you can write all kinds of HTML tags to webpages with the document.write method.

For instance, if you wanted to make a long table that compared Fahrenheit and Celsius, instead of actually typing all the values into the table, you could have javascript calculate the values and write the table to the document.

An example of a javascript generated table can be seen on the page explaining the hexadecimal colorsytem.

On that page, there are 15 tables with 25 columns in each.
Each cell shows different mixtures of the basic colors red, green and blue.

To set up these tables in HTML would demand almost an entire days work. Using javascript for the same purpose took less than 15 minutes!

 << PREVIOUS
READ MORE >>  
















DEVELOPER TIP!





     "Better Than Books - As Easy As It Gets!"