(Redirected from Javascript)
JavaScript is an object-oriented scripting language used in web pages. It was originally developed by Netscape Communications under the name "Mocha" and then "LiveScript" but then renamed to "JavaScript" and given a syntax closer to that of Sun Microsystems' Java language. JavaScript was later standardized by ECMA under the name ECMAScript. The current standard (as of December 1999) is ECMA-262 Edition 3, and corresponds to JavaScript 1.5. Microsoft calls their version JScript.
| Table of contents |
One major use of JavaScript 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.
JavaScript/ECMAScript is implemented by:
application/x-javascript, but the unregistered text/javascript is more commonly used.
To put JavaScript code in an HTML page, it must be preceded with
<script type="text/javascript">and followed with
</script>
For very old browsers, instead use
<script language="JavaScript" type="text/javascript"> <!--and end with
// --> </script>
The <!-- ... --> comment markup is required in order to ensure that the code is not rendered as text by very old browsers which do not recognize the <script> tag in HTML documents, and the LANGUAGE attribute is a deprecated HTML attribute which may be required for old browsers. However, <script> tags in XHTML/XML documents will not work if commented out, as conformant XHTML/XML parsers ignore comments and also may encounter problems with --, < and > signs in scripts (for example, the integer decrement operator and the comparison operators). XHTML documents should therefore have scripts included as a CDATA sections, by preceding with
<script type="text/javascript"> //<![CDATA[and following it with
//]]> </script>
('//' At the start of a line marks a Javascript comment, which prevents the '<![CDATA[' and ']]>' from being parsed by the script.)
HTML elements[1] may contain intrinsic events to which you can associate a script handler. To write valid HTML 4.01, the web server should return a 'Content-Script-Type' with value 'text/javascript'. If the web server cannot be so configured, the website author can optionally insert the following declaration for the default scripting language in the header section of the document.
<meta http-equiv="Content-Script-Type" content="text/javascript" />
Javascript, like HTML, is often not compliant to standards, instead being built to work with specific webbrowser quirks. The current ECMAScript standard should be the base for all Javascript implementations in theory, but in practice the Netscape (and Mozilla) browsers use JavaScript, Microsoft Internet Explorer uses JScript, and other browsers like Opera and Safari use a real ECMAScript implementation, often with additional nonstandard properties to allow compatibility with "Javascript" and "JScript".
JavaScript and JScript contain several properties which are not part of the official ECMAScript standard, and also miss several properties. As such, they are in points incompatible, which requires script authors to work around these bugs. JavaScript is more standards-compliant than Microsoft's JScript, which means that a script file written according to the ECMA standards will work for most browsers, except those based on Internet Explorer.
This also means every browser may treat the same script differently, and what works for one browser may fail in another browser, or even in a different version of the same browser. Like with HTML, it is thus advisable to write standards-compliant code.
MSIE's VBScript is NOT JavaScript, and it is incompatible with the ECMA standard.
Elements may be accessed by numbers or associative names (if defined with them). Thus the following expressions may all be equivalent:
myArray[1], myArray.north, myArray["north"].
Declaration of an array:
myArray = new Array(365);
Arrays are implemented so that only the elements defined use memory; they are "sparse arrays". If I only set myArray[10] = 'someThing' and myArray[57] = 'somethingOther' I have only used space for these two elements.
By defining a constructor function it is possible to define objects. JavaScript 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.
Example: Creating an object
// constructor function
function MyObject(attributeA, attributeB) {
this.attributeA = attributeA
this.attributeB = attributeB
}
// create an Object
obj = new MyObject('red', 1000)
// access an attribute of obj alert(obj.attributeA)
// access an attribute with the associative array notation alert(obj["attributeA"])
Object hierarchy can be emulated in JavaScript. For example:
function Base()
{
this.Override = _Override;
this.BaseFunction = _BaseFunction;
function _Override()
{
alert("Base::Override()");
}
function _BaseFunction()
{
alert("Base::BaseFunction()");
}
}
function Derive()
{
this.Override = _Override;
function _Override()
{
alert("Derive::Override()");
}
}
Derive.prototype = new Base();
d = new Derive(); d.Override(); d.BaseFunction();
will result in the display:
Derive::Override() Base::BaseFunction()
if (condition) {
statements
}
[else {
statements
}]
while (condition) {
statements
}
do {
statements
} while (condition);
for ([initial-expression]; [condition]; [increment-expression]) {
statements
}
for (variable in object) {
statement
}
switch (expression) {
case label1 :
statements;
break;
case label2 :
statements;
break;
default :
statements;
}
function(arg1, arg2, arg3) {
statements;
return expression;
}
Example: Euclid's original algorithm of finding the greatest common divisor. (This is a geometrical solution which subtracts the longer segment from the shorter):
function gcd(segmentA, segmentB)
{
while(segmentA!=segmentB)
if(segmentA>segmentB)
segmentA -= segmentB;
else
segmentB -= segmentA;
return(segmentA); }
The number of arguments given when calling a function must not necessarily accord to the number of arguments in the function definition. Within the function the arguments may as well be accessed through the arguments array.
Every function is an instance of Function, a type of base object. Functions can be created and assigned like any other objects:
var myFunc1 = new Function("alert('Hello')");
var myFunc2 = myFunc1;
myFunc2();
results in the display:
Hello
List of events
1 Blur is when the object is deselected or something else is selected instead.
2 Focus is when the object is selected, usually in a form.
See also http://tech.irt.org/articles/js058/
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
}
The programming language used in Macromedia Flash (called ActionScript) bears a strong resemblance to JavaScript due to their shared relationship with ECMAScript. ActionScript has nearly the same syntax as JavaScript, but the object model is dramatically different.
| Programming Languages |
| Ada | Algol | APL | BASIC | COBOL | C | C++ | C# | ColdFusion | Delphi | Eiffel | Forth | FORTRAN | Haskell | Java | JavaScript | Jython | Lisp | ML | Modula-2 | Oberon | Pascal | Objective-C | Perl | PHP | PL/I | PostScript | Powerbuilder | Prolog | Python | QBASIC | Ruby | Scheme | Smalltalk | Tcl/Tk | Visual Basic |
![]() Linda Goodman's Sun Signs |
![]() Robin MacNaughton's Sun Sign Personality Guide : A Complete Love and Compatibility Guide for Every Sign in the Zodiac |
![]() Llewellyn's 2004 Sun Sign Book: Horoscopes for Everyone (Llewellyn's Sun Sign Book) |
![]() Black Sun Signs: An African-American Guide to the Zodiac | ||||
![]() Baby Signs: How to Talk with Your Baby Before Your Baby Can Talk, New Edition |
![]() Treasure Signs, Symbols, Shadow and Sun Signs |
![]() Soul Signs : Harness the Power of Your Sun Sign and Become the Person You Were Meant to Be |
![]() A Raisin in the Sun and The Sign in Sidney Brustein's Window | ||||
![]() Zodiac Spells: Easy Enchantments and Simple Spells for Your Sun Sign |