Jump to content

JavaScript

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by AxelBoldt (talk | contribs) at 19:22, 29 December 2001 (something I ran into while trying to write bookmarklets...). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The ECMAScript programming language, commonly known as JavaScript, is an object-oriented scripting language specified by the ECMA-262 standard.

Netscape Communications originally developed the language under the name "LiveScript" but then renamed it to "JavaScript" and gave it a syntax closer to that of the Java programming language developed by Sun Microsystems. This happened at roughly the same time Netscape was including support for Java technology in its Netscape Navigator browser.


ECMAScript is an object-oriented scripting language that connects through interfaces called Document Object Models (DOMs) to applications,

especially to the server side (web servers) and the client side (Internet browsers) of internet applications.

Many web sites use client-side ECMAScript technology to create powerful dynamic web applications.

It may use Unicode and can evalutate regular expressions and ECMAScript expressions contained in a string (eval function).


Scope of Standardization


The language itself as described below is standardized, and web browsers usually adhere to the standard. One major use of ECMAScript is to write little functions that are embedded in HTML pages and interact with the DOM of the browser to perform certain tasks not possible in static HTML alone, such as opening a new window, checking input values, changing images as the mouse cursor moves over etc. Unfortunately, the DOMs of browsers are not standardized, different browsers expose different objects or methods to the script, and it is therefore often necessary to write different variants of a Javascript function for the various browsers.


Environment


To put ECMAScript code in an HTML page, precede it with

<script type="text/javascript"><!--

and follow it with

// --></script>


As JavaScript is the default scripting language in newer browsers, and old browsers which are confused with code showing up are rarely used anymore, it suffices to write


<script> ... some JavaScript code ... </script>



Variables


Variables are generally dynamically typed.

Variables are defined by either just assiging them a value or by using the var statement.

Variables declared outside of any function are in "global" scope, visible in the entire web page; variables declared inside a function are local to that function.

To pass variables from one page to another, a developer can set a cookie or use a hidden frame or window in the background to store them.



Data structures


The primary data structure is an associative array similar to hashes in the Perl programming language.

Elements may be accessed by numbers or associative names.

Thus the following expressions may all be equivalent: myArray[1], myArray.north, myArray["north"].


Objects


ECMAScript has several kinds of built in objects, namelly Object, Array, String, Date and Math.

Ohter objects belong to the DOM (window, form, links etc.).


By defining a constructor function it is possible do define objects.

ECMAScript is a prototype based object-oriented language.

One can add additional properties or methods to individual objects after they have been created.

To do this for all instances of a certain object type one can use the prototype statement.




Control structures


If ... else


if (condition)

   { statements1}

[else { statements2}]



While loop


while (condition)

     {   statements}


Do ... while


do

{statements}

while (condition);


For loop


for ([initial-expression;] [condition;] [increment-expression])
 {   statements}



For ... in loop


for(variable in object) 

   {statements}

This loop goes through all properties of an object (or elements of an array).



Switch expression


switch (expression)
{   case label :       statement;  
    break;   
    case label :       statement;
    break; 
    ... 
    default : statement;}



User interaction


Most interaction with the user is done by using HTML forms which can be accessed from Javascript. However there are as well some very simple means of communicating with the user

  • Alert dialog box
  • Confirm dialog box
  • Prompt dialog box
  • Status bar



Error handling


Newer versions of Javascript (as used in Internet Explorer 5 and Netscape 6) include a try ... catch error handling statement.


The try...catch...finally statement catches exceptions resulting from an error or a throw statement. Its syntax is as follows:


try {

  // Statements in which exceptions might be thrown

} catch(error) {

  // Statements that execute in the event of an exception

} finally {

  // Statements that execute afterward either way

}


Initially, the statements within the try block execute. If an exception is thrown, the script's control flow immediately transfers to the statements in the catch block, with the exception available as the error argument. Otherwise the catch block is skipped. Once the catch block finishes, or the try block finishes with no exceptions thrown, the statements in the finally block execute. This figure summarizes the operation of a try...catch...finally statement:




Here's a script that shows try...catch...finally in action step by step.


try {

    statements

  }

  catch (err) {

    // handle error

  }

  finally {

    statements

  } 



The finally part may be omitted.


try {

    statements

  }

  catch (err) {

    // handle error

  }



External links:


/Talk