Friday, September 5, 2008

Introducing to JavaScript

It's important to understand the difference between Java and JavaScript. Java is a full programming language developed by Sun Microsystems with formal structures, etc. JavaScript is a scripting language developed by Netscape that is used to modify web pages. Most JavaScript must be written in the HTML document between <SCRIPT> tags. You open with a <SCRIPT> tag, write your JavaScript, and write a
closing </SCRIPT> tag. Sometimes, as an attribute to script, you may add “Language=JavaScript” because there are other scripting languages as well as JavaScript that can be used in HTML. We’ll go through some examples to demonstrate the syntax of JavaScript.

To understand the workings of JavaScript, it is essential to understand a few basic programming concepts. JavaScript is object-oriented. An Object in JavaScript is a resource that has specific characteristics known as properties and provides several services known as methods and events. An example of an object is document, which represents the current web page and has properties such as location ( which stores the URL location of the document) and methods such as writeln , which writes dynamically created html text to the document.

A variable stores a value. It can be thought of as a labeled box, with the name of the variable as the label
and the value as the contents. The JavaScript statement:

var x="hello";

assigns to the variable named x the String value "hello".

var x=1;

This line of JavaScript assigns to the variable x the integer value 1. As you can see, a JavaScript variable can refer to a value of any type; this can be integer, string, or even any type of object. You dont have to specify the type of variable before creating it. Note that object properties can be thought of as variables that belong to the object.

A method is basically a collection of statements that does something. For example, a methodwriteln()” exists in the document object that can be used to write html to your document. Methods are predefined in JavaScript. It is possible for you to define functions, which can be thought of as methods you define outside of any object.

When you have the syntax object.method as you do with document.writeln, the method operates on the object given. In this case, the writeln method operates (the operation is writing) to the document (the browser window that you see). This syntactic structure is often used in JavaScript.