Make your own free website on Tripod.com

Chapter 2: JavaScript Basics

 

2.1 The syntax of JavaScript

JavaScript does not required any special program to run it, you can use any standard text editors such as the Notepad ,WordPad or FrontPage to type it, as it is embedded within an HTML document. To embed a JavaScript into an HTML document, we use the <SCRIPT  language="JavaScript></Script> tag. Let me illustrate one example here:

<HTML>

<Body>

<SCRIPT  language="JavaScript>

document.write ("Welcome to JavaScript tutorial");

</SCRIPT>

</HTML>

The output is as below:

 

 

document is an object in JavaScript and write is a method that display text in an HTML document. The statement must end with a semicolon ;. so, document.write will display the text enclosed in the brackets on the HTML document. You can also enhance the text output by changing the font size, style, color and so on. The following example will output the text as a Heading.

 

<HTML>

<Body>

<SCRIPT  language="JavaScript">

document.write ("<H1>Welcome to JavaScript tutorial</H1>");

</SCRIPT>

</HTML>

 

This example will add color to the text

 

<HTML>

<Body>

<SCRIPT  language="JavaScript">

document.write ("<Font color='Red'><H1>Welcome to JavaScript tutorial</H1></Font>");

</SCRIPT>

</HTML>

The output is as follows:

Another JavaScript method that is associated with displaying text in an HTML document is the writeln method. The writeln method pushes the cursor to the next line while the write method will put the cursor next to the last character. However, the actual rendering of the HTML text does not start a new line even though we use the writeln method. So, basically write and writeln are the same. If you want to start a new line, just use <P> or the <br> tag. Let's see the two examples below:

<HTML>

<Body>

<SCRIPT  language="JavaScript">

document.write ("<Font color='Red'><H1>Welcome to ");

document.writeln ("JavaScript tutorial</H1></Font>");

</SCRIPT>

</HTML>

the output :

 

<HTML>

<Body>

<SCRIPT  language="JavaScript">

document.writeln ("<Font color='Red'><H1>Welcome to <br>");

document.write ("JavaScript tutorial</H1></Font>");

</SCRIPT>

</HTML>

The output:

Note that <br> can be embeded into a JavaScript statement, and each statement must be separated by a semicolon;

I think you have learned some basic concepts of JavaScript and now is the time for you to do some experiments on your own. You copy the codes here and modify them according to your taste. Learning by doing is the best form of learning! Let finish here and come back later for the next Chapter.

 

 

 

 

<Chapter 1><Chapter 3>

[Back to HTMLTutorial]