JavaScript is a relatively simple programming language for computer documents that
adds significantly greater interactivity to their basic layout.
JavaScript is not a general-purpose programming language,
but instead is designed to work within the realm of computer documents.
- JavaScript is an interpreted, object-oriented programming language,
having its roots in C++ and Java, e.g.:
document.images[0].height++ |
JavaScript also incorporates some aspects of Apple HyperCard's HyperTalk language:
<form> <input name="College" type="button"
value="Amherst College" onClick="showLogo(this)">
</form> |
|
- As its name implies, JavaScript is a "scripting" language like HyperTalk, meaning it removes a lot of the complexity of programming by making many basic assumptions.
It is therefore much simpler than either C++ or Java.
It also has much less overhead than Java, so its use is preferred when possible.
When Java is used, JavaScript can greatly enhance its interactivity because of its ability to communicate with Java.
- Like PHP, JavaScript statements live "within" the layout language of a computer document.
- JavaScript statements can be part of attribute/value pairs within layout tags, as for the
onClick
event handler above;
- JavaScript statements can also be in the head or body of the document:
<body>
....
The time is now <strong>
<script language="JavaScript">
var now = new Date
document.write(now.getHours() +
":" + now.getMinutes() + '.')
</script>
</strong> Are you on time?
....
</body> |
The time is now
Are you on time?
|
Unlike PHP, JavaScript statements are executed as they are loaded by the browser, so sequencing relative to the HTML is more important.
- JavaScript can run both on the server (similar to PHP) and on the client, i.e. the browser.
We will concentrate on the latter here, as it allows a desireable localization of many activities e.g. testing of form inputs:
<form>
<input name="College" type="text" value="Type Your College Here"
size="22" onChange="checkCollege(this)">
</form> |
|
(Plus, server-side JavaScript is not commonly available.)
- JavaScript has many limitations designed to protect users from surreptitious activities on the client computer.
For example, it cannot:
- Access (read or modify) browser settings;
- Print, launch applications, or send email;
- Access files or directories.
- Though most commonly used with documents written in HyperText Markup Language (HTML), i.e. "web pages", JavaScript is generally useful for any computer document format that allows for it, for example:
JavaScript has a long and sordid history (not!).
- JavaScript was created by Netscape's Brendan Eich in 1995.
It originally shipped with Netscape Navigator 2.0 in early 1996.
It subsequently was incorporated into Microsoft's Internet Explorer 3.0.
- JavaScript's original name was LiveScript, but it was changed to take advantage of the Java hype that was occuring around the time of its introduction.
Microsoft's implementation of JavaScript is called JScript (due to trademark issues).
The core language of JavaScript (the part that is document independent) is also now a standard of the European Computer Manufacturer's Association, ECMA-262, and so is known as ECMAScript.
- Almost every new version of a browser comes with a new implementation of JavaScript, and a new set of capabilities that enable it to work with both older and newer versions of HTML:
Corresponding Language and Browser Versions
|
JavaScript |
JScript |
ECMA-262 |
|
Netscape
Navigator |
MS Internet
Explorer |
Mozilla |
Apple
Safari (KJS) |
1.0 |
1.0 |
|
|
2 |
3.0 |
|
|
1.1 |
2.0 |
1 |
|
3 |
3.? |
|
|
1.2 |
3.0 |
1 |
|
4.0 |
4.0 |
|
|
1.3 |
5.0 |
1 |
|
4.06 |
5.0 |
|
|
1.4 |
5.5 |
3 |
|
|
5.5 |
|
|
1.5 |
5.6 |
3 |
|
6 |
6.0 |
1.0 |
1.0 |
Your browser will, by default, use the latest version of JavaScript that it knows about.
When characteristics of an earlier version of JavaScript are needed, one must specifically request that version:
<script language="JavaScript1.1"> ....
</script> |
- While the core language of JavaScript is essentially the same for the various HTML platforms (each OS/browser combination), there can be significant differences:
- in the document models they use, i.e. the way document elements such as buttons are referenced;
- in some of the features they provide.
For example, the ability to script images became available in Netscape Navigator 3, but didn't appear in Internet Explorer until version 4:
<a href="http://www.amherst.edu/" onMouseOver="amherstIMG.src = 'jlamherst.jpg'; return true" onMouseOut="amherstIMG.src = 'ljamherst.jpg'; return true">
<img src="ljamherst.jpg" name="amherstIMG"
alt="Amherst College" border="0">
</a>
|
|
In addition, newer technologies that JavaScript can control, such as Cascading Style Sheets, may have inconsistent implementations (CSS was introduced in Netscape Navigator 4 and in later versions of Internet Explorer 4, but they still have some disagreements in NN 7 and IE 6!):
<p style="text-align: center; background: #FCF;"
onmouseover="this.style.background = '#C6F'"
onmouseout="this.style.background = '#FCF'">
Lord Jeffrey Amherst (1717 - 1797)<br>
Painting by Sir Joshua Reynolds (1723 - 1792)<br>
Scanned from
<a href="http://fclibr.library.umass.edu/...">
<cite>Old Quebec, the Fortress of New France</cite>
</a>
by
<a href="http://www.blupete.com/...">
Peter Landry
</a>
</p> |
Lord Jeffrey Amherst (1717 - 1797)
Painting by Sir Joshua Reynolds (1723 - 1792)
Scanned from Old Quebec, the Fortress of New France
by Peter Landry
|
One must therefore be very careful when using JavaScript to maximize cross-platform compatibility for your target audience.
There are lots of good sources of information about JavaScript.
- There are many books available on JavaScript; for example:
- JavaScript Bible, 4th ed., by Danny Goodman (John Wiley & Sons, 2001)
- JavaScript Definitive Guide, 4th ed., by David Flanagan (O'Reilly & Associates, 2001)
- JavaScript Examples Bible, by Danny Goodman (John Wiley & Sons, 2001)
- JavaScript & DHTML Cookbook, by Danny Goodman (O'Reilly & Associates, 2003)
- JavaScript for the World Wide Web: Visual QuickStart Guide, 5th ed.,
by Tom Negrino and Dori Smith (Peachpit Press, 2003) - Extending Acrobat Forms with JavaScript, by John Deubert (Peachpit Press, 2003)
- JavaScript Pocket Reference, 2nd ed., by David Flanagan (O'Reilly & Associates, 2002)
This last one is also available through
Dreamweaver > Help > Reference > O'REILLY JavaScript Reference
.
- Many DHTML references also spend some time discussing JavaScript, e.g.:
- There are also some on-line references, tutorials, and discussion groups, e.g.:
- JavaScript source code that you can use in your pages can be found on the CDs
that come with most books nowadays.
Dreamweaver also has many ready-to-insert scripts, accessible through
Dreamweaver > Window > Behaviors
.
There are also many online repositories, e.g.:
Be aware that many of the above scripts are not tested for cross-platform compatibility (e.g. they may work with IE in Windows but not anywhere else).