Thursday, March 13, 2008

JavaScript Statements

A JavaScript statements is a command to the browser. The purpose of the command is to tell the browser what to do.

This JavaScript statement tells the browser to write "Hello Dolly" to the web page:




It is normal to add a semicolon at the end of each executable statement. Most people think this is a good programming practice, and most often you will see this in JavaScript examples on the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.
Note: Using semicolons makes it possible to write multiple statements on one line.

JavaScript Code:


JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will write a header and two paragraphs to a web page:






JavaScript Blocks:


JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together. This example will write a header and two paragraphs to a web page:


Where to Put the JavaScript

JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.

Scripts in the head section: Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it.








Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.










Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.











Using an External JavaScript
Sometimes you might want to run the same JavaScript on several pages, without having to write the same script on every page.
To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the 'script' tag.
To use the external script, point to the .js file in the "src" attribute of the script tag:


Note: Remember to place the script exactly where you normally would write the script!

How to Put a JavaScript Into an HTML Page






The code above will produce this output on an HTML page:
"Hello World!"


Example Explained


To insert a JavaScript into an HTML page, we use the script tag. Inside the script tag we use the "type=" attribute to define the scripting language.
So, the script type= text/javascript and script tells where the JavaScript starts and ends:


The word document.write is a standard JavaScript command for writing output to a page. By entering the document.write command between the tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page.






Note: If we had not entered the 'script' tag, the browser would have treated the document.write("Hello World!") command as pure text, and just write the entire line on the page.