Friday, March 19, 2010

Javascript Basics

What is JavaScript?

  • JavaScript was designed to add interactivity to HTML pages
  • JavaScript is a scripting language
  • A scripting language is a lightweight programming language
  • A JavaScript consists of lines of executable computer code
  • A JavaScript is usually embedded directly into HTML pages
  • JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
  • Everyone can use JavaScript without purchasing a license
  • this is a case sensitive scripting language
  • based on OOP paradigm of Programming Languages
JavaScript is used in millions of Web pages to improve the design, validate forms, detect browsers, create cookies, and much more.
Since JavaScript is currently the only scripting language supported by every major web browser (Internet Explorer, Firefox, Netscape, Safari, Opera, Camino, etc), it is very widely used. When code is rendered by your web browser, like JavaScript usually is, it is called a Client-Side script. JavaScript can also be run on a web server to generate HTML documents, thus running as a Server-Side script. "JavaScript cannot be a standalone application instead it an Internet based application embedded in a web page & executed on a JavaScript compatible browser." Javascript is capable of manipulating data as well as Events {Event: any action on browser, e.g. OnmouseClick etc.}. The script is actually just some commands that the browser has to do.

What can JavaScript do?

  • Provide Interactivity to HTML elements.
  • A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing.
  • Can be used to read/write i.e change html element's data.
  • Manipulate Events.
  • Detect Browser so as to fire Browser specific code.
  • A JavaScript can be used to store and retrieve information on the visitor's computer, this can be done with the help of Javascript Cookies.
  • Provide dynamic functionality to a web site.
  • Can develop Client-Side Script oriented or Server-Side Script Oriented Internet Applications.

Toolset Required for JavaScript:

  • Javascriptcompatible Browser.
  • Any Editor e.g. Notepad, Editplus etc.

Prerequisite Knowledge Required:

Prior knowledge of HTML/XHTML is required as a prerequisite to the knowledge of Javascript.

Syntax:

First, we need to know how to add JavaScript to an HTML page. JavaScript can be added in one of two ways:
  1. You can place Script tags in your webpage and place the JavaScript code inside of those.

    <script type="text/javascript">

    ... block of code ....

    </script>
  2. You can place all of your JavaScript code in another file and link to it with a Script tag.

    <script type="text/javascript" src="scripts/YourJavaScriptFile.js"></script>
<script>...</script>: Interprets to browser that some sort of script is to be encountered.

Type=””: attribute of script element to describe which scripting language to be used.

Src=”<path of js file..>”: attribute of script element to mention source path of .js file.

Commenting Styles:

  1. <!-- code to be commented - ->
  2. // code to be commented
  3. /* code to be commented */

JavaScript Where To ...:

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

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.

JavaScript Statements:

A JavaScript statements is a command to the browser. The purpose of the command is to tell the browser what to do.
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 Variables:

As with algebra, JavaScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more describing name like length. A JavaScript variable can also hold a text value like in carname="Volvo".

Rules for JavaScript variable names:
* Variable names are case sensitive (y and Y are two different variables)
* Variable names must begin with a letter or the underscore character
NOTE: Because JavaScript is case-sensitive, variable names are case-sensitive.
Syntax for Declaring (Creating) JavaScript Variables
var statement;

More Details about JavaScript will be posted in my next article....

No comments:

Post a Comment