javascript
introduction
javascript is a scripting language widely used for client-side web development. it was the originating dialect of the ecmascript standard. it is a dynamic, weakly typed, prototype-based language with first-class functions. javascript was influenced by many languages and was designed to look like java, but be easier for non-programmers to work with.
although best known for its use in websites (as client-side javascript), javascript is also used to enable scripting access to objects embedded in other applications (see below).
javascript, despite the name, is essentially unrelated to the java programming language, although both have the common c syntax, and javascript copies many java names and naming conventions. the language’s name is the result of a co-marketing deal between netscape and sun, in exchange for netscape bundling sun’s java runtime with their then-dominant browser. the key design principles within javascript are inherited from the self and scheme programming languages.
“javascript” is a trademark of sun microsystems. it was used under license for technology invented and implemented by netscape communications and current entities such as the mozilla foundation.
history and naming
javascript was originally developed by brendan eich of netscape under the name mocha, which was later renamed to livescript, and finally 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 1995. the naming has caused confusion, giving the impression that the language is a spin-off of java, and it has been characterized by many as a marketing ploy by netscape to give javascript the cachet of what was then the hot new web-programming language.
microsoft named its dialect of the language jscript to avoid trademark issues. jscript was first supported in internet explorer version 3.0, released in august 1996, and it included y2k-compliant date functions, unlike those based on java.util.date in javascript at the time. the dialects are perceived to be so similar that the terms “javascript” and “jscript” are often used interchangeably (including in this article). microsoft, however, notes dozens of ways in which jscript is not ecma compliant.
netscape submitted javascript to ecma international for standardization resulting in the standardized version named ecmascript.
main uses
the primary use of 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. some simple examples of this usage are:
- opening or popping up a new window with programmatic control over the size, position, and attributes of the new window (i.e. whether the menus, toolbars, etc. are visible).
- validation of web form input values to make sure that they will be accepted before they are submitted to the server.
- changing images as the mouse cursor moves over them: this effect is often used to draw the user’s attention to important links displayed as graphical elements.
because javascript code can run locally in a user’s browser (rather than on a remote server) it can respond to user actions quickly, making an application feel more responsive. furthermore, javascript code can detect user actions which html alone cannot, such as individual keystrokes. applications such as gmail take advantage of this: much of the user-interface logic is written in javascript, and javascript dispatches requests for information (such as the content of an e-mail message) to the server. the wider trend of ajax programming similarly exploits this strength.
a javascript engine (also known as javascript interpreter or javascript implementation) is an interpreter that interprets javascript source code and executes the script accordingly. the first ever javascript engine was created by brendan eich at netscape communications corporation, for the netscape navigator web browser. the engine, code-named spidermonkey, is implemented in c. it has since been updated (in javascript 1.5) to conform to ecma-262 edition 3. the rhino engine, created primarily by norris boyd (also at netscape) is a javascript implementation in java. rhino, like spidermonkey, is ecma-262 edition 3 compliant.
the most common host environment for javascript is by far a web browser. web browsers typically use the public api to create “host objects” responsible for reflecting the dom into javascript. the web server is another common application of the engine. a javascript webserver would expose host objects representing an http request and response objects, which a javascript program could then manipulate to dynamically generate web pages.
things to consider
the dom interfaces for manipulating web pages are not part of the ecmascript standard, or of javascript itself. officially, they are defined by a separate standardization effort by the w3c; in practice, browser implementations differ from the standards and from each other, and not all browsers execute javascript.
to deal with these differences, javascript authors can attempt to write standards-compliant code which will also be executed correctly by most browsers; failing that, they can write code that checks for the presence of certain browser features and behaves differently if they are not available. in some cases, two browsers may both implement a feature but with different behavior, and authors may find it practical to detect what browser is running and change their script’s behavior to match. programmers may also use libraries or toolkits which take browser differences into account.
security
javascript and the dom provide the potential for malicious authors to deliver scripts to run on a client computer via the web. browser authors contain this risk using two restrictions. first, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site. most javascript-related security bugs are breaches of either the same origin policy or the sandbox.
one common javascript-related security problem is cross-site scripting, or xss, a violation of the same-origin policy. xss vulnerabilities occur when an attacker is able to cause a trusted web site, such as an online banking website, to include a malicious script in the webpage presented to a victim. the script in this example can then access the banking application with the privileges of the victim, potentially disclosing secret information or transferring money without the victim’s authorization.
xss vulnerabilities can also occur because of implementation mistakes by browser authors.
xss is related to cross-site request forgery or xsrf. in xsrf one website causes a victim’s browser to generate fraudulent requests to another site with the victim’s legitimate http cookies attached to the request.




