Javascript

From The Ultimate Programming Reference

JavaScript is the name of Netscape's implementation of ECMAScript, a scripting programming language based on the concept of prototypes. The language is best known for its use in websites, but is also used to enable scripting access to objects embedded in other applications.

Despite the name, JavaScript is only distantly related to the Java programming language, the main similarity being their common debt to the C programming language. JavaScript has far more in common with the Self programming language.

JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. [1] [2]

Contents

[edit] History

JavaScript was originally developed by Brendan Eich of Netscape Communications Corporation under the name Mocha, then LiveScript, and finally renamed to JavaScript. The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. JavaScript was first introduced and deployed in the Netscape browser version 2.0B3 in December of 1995. When web developers talk about using JavaScript in Internet Explorer, they are actually using JScript. The choice of name proved to be a source of much confusion.

As of 2005, the latest version of the language is JavaScript 1.6, which corresponds to ECMA-262 Edition 3 like JavaScript 1.5, except for Array extras, and Array and String generics. ECMAScript, in simple terms, is a standardized version of JavaScript. The ECMA-357 standard specifies E4X, a language extension dealing with XML.

[edit] Usage

JavaScript is a prototype-based scripting language with a syntax loosely based on C. Like C, the language has no input or output constructs of its own. Where C relies on standard I/O libraries, a JavaScript engine relies on a host environment into which it is embedded. There are many such host environment applications, of which web technologies are the most well known examples. These are examined first.

One major use of web-based JavaScript is to write functions that are embedded in or included from HTML pages and interact with the Document Object Model (DOM) of the page to perform tasks not possible in HTML alone. Some common examples of this usage follow.

Opening or popping up a new window with programmatic control over the size, position and 'look' of the new window (i.e. whether or not the menus, toolbars etc are visible. Usually JavaScript is used to ensure that they are not)
If this degree of control is not required, JavaScript is not necessary. Simply adding the attribute target="xxx" to the link element in HTML will reliably produce a new window the same size as the current one, with menus etc displayed as per the user's preferences. Note that many browsers now include mechanisms that, by default, block all JavaScript pop-ups, displaying only a small message to say that they have done so.
Checking or validating web form input values to make sure that they will be accepted before they are submitted to the server
There is always a time delay, and a processing overhead on the server, when a form has to be submitted. Nonetheless input validation should be repeated at the server in case the JavaScript failed to run (see below).
Changing images as the mouse cursor moves over them
This effect is still enjoyed by many designers, often to draw the user's attention to important links displayed as graphical elements. This "rollover" effect can also be implemented with CSS in recent browsers. With a mind for visually impaired users, who may be using extreme text magnification that makes non-resizing graphics largely irrelevant, others are moving away from this approach.

Unfortunately, the DOM interfaces in various browsers differ and don't always match the W3C DOM standards. Rather than write different variants of a JavaScript function for each of the many browsers in common use today, it is usually possible, by carefully following the W3C DOM Level 1 or 2 standards, to provide the required functionality in a standards-compliant way that most browsers will execute correctly. Care must always be taken to ensure that the web page degrades gracefully and so is still usable by any user who

  • has JavaScript execution disabled - for example as a security precaution
  • has a browser that does not understand the JavaScript - for example on a PDA or mobile phone
  • is visually or otherwise disabled and may be using an unusual browser, a speech browser or may have selected extreme text magnification. For more information on this, see the Web Accessibility Initiative

Other examples of JavaScript interacting with a web page's DOM have been called DHTML and SPA.

A different example of the use of JavaScript in web pages is to make calls to web and web-service servers after the page has loaded, depending upon user actions. These calls can obtain new information, which further JavaScript can merge with the existing page's DOM so that it is displayed. This is the basis of Ajax programming.

Outside of the Web, JavaScript interpreters are embedded in a number of tools. Adobe Acrobat and Adobe Reader support JavaScript in PDF files. The Mozilla platform, which underlies several common web browsers, uses JavaScript to implement the user interface and transaction logic of its various products. JavaScript interpreters are also embedded in proprietary applications that lack scriptable interfaces. Dashboard Widgets in Apple's Mac OS X v10.4 are implemented using JavaScript. Microsoft's Active Scripting technology supports JavaScript-compatible JScript as an operating system scripting language. JScript .NET is a CLI-compliant language that is similar to JScript, but has further object oriented programming features. Tools in the Adobe Creative Suite, including Photoshop, allow scripting through JavaScript.

Each of these applications provides its own object model which provides access to the host environment, with the core JavaScript language remaining mostly the same in each application.

[edit] Syntax

Main article: JavaScript syntax

[edit] Debugging

JavaScript debugging has some specifics in comparison with stay-alone applications. First of all JavaScript is an interpreted language so run-time errors do not appear until the relevant program block is called. Secondly JavaScript programs are usually heavy on communication with Document Object Model (DOM) so an error may be caused by wrong DOM usage in technically correct script. All this may make difficult to track the cause for particular errors. However nowadays both Internet Explorer and the Gecko-based browsers come with a reasonably good debugger. Gecko browsers use the Venkman debugger (free), while for Internet Explorer there is the Microsoft Script Debugger (free). Also Microsoft provides Microsoft Script Editor (MSE) as part of their Microsoft Office package (Microsoft Office 2002 or higher). There are also free online cheking tools like JSLint/.

Scripting languages are especially susceptible to bugs for the inexperienced programmer. Because JavaScript is interpreted, loosely-typed, and has varying environments (host applications), implementations and versions the programmer has to take extra care to make sure the code executes as expected.

Each script block is parsed separately. On pages where JavaScript in script blocks is mixed with HTML, syntax errors can be identified more readily by keeping discrete functions in separate script blocks, or (for preference), using many small linked .js files. This way, a syntax error will not cause parsing/compiling to fail for the whole page, and can enable a dignified die.

[edit] Related languages

There is no real relationship between Java and JavaScript; their similarities are mostly in basic syntax because both are ultimately derived from C. Their semantics are quite different and their object models are unrelated and largely incompatible. Like C and C++, all Java variables are strongly typed, whereas all JavaScript variables are variants, declared as type var, that may refer to strings, numbers, booleans, objects etc interchangeably. It is also worth mentioning Microsoft's own VBScript, which, like JavaScript, can be run client-side in web pages. VBScript has syntax derived from Visual Basic and will only run if the web pages are viewed in Internet Explorer. Even server-side or outside of web pages, VBScript is only useful on the Microsoft platform.

Due to the success of JavaScript, Microsoft developed a compatible language, which it called JScript. JScript was first supported in Internet Explorer version 3.0, released in August, 1996; when web developers talk about the use of JavaScript in the IE browser, they usually actually mean JScript.

The need for common specifications for these two languages was the basis of the ECMA 262 standard for ECMAScript (see external links below), three editions of which have been published since the work started in November 1996. The object model of browser-based JavaScript, the Document Object Model (DOM), is actually not part of the ECMAScript standard. It is defined in a set of separate standards[3], developed by the W3C and is applicable to the access and manipulation of HTML and XML documents in many computer languages and platforms.

ActionScript, the programming language used in Macromedia Flash, bears a resemblance to JavaScript, because of a common ancestry in the ECMAScript standards. Although ActionScript has a similar syntax to JavaScript, the object model is again dramatically different.

JSON, or JavaScript Object Notation, is a general-purpose data interchange format that is defined as a subset of JavaScript.

JavaScript OSA (JavaScript for OSA, or JSOSA), is a Macintosh scripting language based on the Mozilla 1.5 JavaScript implementation, SpiderMonkey. It is a freeware component made available by Late Night Software. Interaction with the operating system and with third-party applications is scripted via a MacOS object. Otherwise, the language is virtually identical to the core Mozilla implementation. It was offered as an alternative to the more commonly used AppleScript language.

Of only historical interest now, ECMAScript was included in the VRML97 standard for scripting nodes of VRML scene description files.

JavaScript is in fact rather similar in semantics to the functional programming languages such as Scheme and Ocaml, having closures and supporting higher-order functions. The Little Javascripter shows the relationship with Scheme in more detail.

[edit] See also

Template:Wikibookspar

[edit] References

  • Nigel McFarlane: Rapid Application Development with Mozilla, Prentice Hall Professional Technical References, ISBN 0131423436
  • David Flanagan, Paula Ferguson: JavaScript: The Definitive Guide, O'Reilly & Associates, ISBN 0596000480
  • Danny Goodman, Scott Markel: JavaScript and DHTML Cookbook, O'Reilly & Associates, ISBN 0596004672
  • Danny Goodman, Brendan Eich: JavaScript Bible, Wiley, John & Sons, ISBN 0764533428
  • Andrew H. Watt, Jinjer L. Simon, Jonathan Watt: Teach Yourself JavaScript in 21 Days, Pearson Education, ISBN 0672322978
  • Thomas A. Powell, Fritz Schneider: JavaScript: The Complete Reference, McGraw-Hill Companies, ISBN 0072191279
  • Scott Duffy: How to do Everything with JavaScript, Osborne, ISBN 0072228873
  • Andy Harris, Andrew Harris: JavaScript Programming, Premier Press, ISBN 0761534105
  • Joe Burns, Andree S. Growney, Andree Growney: JavaScript Goodies, Pearson Education, ISBN 0789726122
  • Gary B. Shelly, Thomas J. Cashman, William J. Dorin, Jeffrey Quasney: JavaScript: Complete Concepts and Techniques, Course Technology, ISBN 0789562332
  • Nick Heinle, Richard Koman: Designing with JavaScript, O'Reilly & Associates, ISBN 1565923006
  • Sham Bhangal, Tomasz Jankowski: Foundation Web Design: Essential HTML, JavaScript, CSS, PhotoShop, Fireworks, and Flash, APress L. P., ISBN 1590591526
  • Emily Vander Veer: JavaScript For Dummies, 4th Edition, Wiley, ISBN 0764576593

[edit] External links

Personal tools