JavaScript

Submitted by Alan on Sat, 04/25/2009 - 20:08

JavaScript is a cross-platform, object-based scripting language for client and server applications. JavaScript lets you create applications that run over the Internet. Using JavaScript, you can create dynamic xHTML pages that process user input and maintain persistent data using special objects, files, and relational databases.

JavaScript statements can be embedded directly in an xHTML page. These statements can recognize and respond to user events such as mouse clicks, form input, and page navigation. For example, you can write a JavaScript function to verify that users enter valid information into a form.

Without any network transmission, an xHTML page with embedded JavaScript can interpret the entered text and alert the user with a message dialog if the input is invalid. Or you can use JavaScript to perform an action (such as play an audio file, execute an applet, or communicate with a plug-in) in response to the user opening or exiting a page.

JavaScript is a programmable API that allows cross-platform scripting of events, objects, and actions. It allows the page designer to access events such as startups, exits, and users' mouse clicks. JavaScript extends the programmatic capabilities of Netscape Navigator (and to a slightly lesser extent, Microsoft's Internet Explorer) to a wide range of authors, and is easy enough for anyone who can compose xHTML.

Using JavaScript, even less-experienced developers will be able to direct responses from a variety of events, objects, and actions. It provides anyone who can compose xHTML with the ability to change images and play different sounds in response to specified events, such as a users' mouse click or screen exit and entry.

There are several versions of JavaScript supported by certain browsers and browser versions. This can often lead to confusion and incompatibilities. Since Netscape originally introduced JavaScript, JavaScript 1.0 was the language specification supported in Netscape Navigator 2.0. Subsequently, Navigator 3.0 supported new enhancements which comprised JavaScript 1.1. At present, Navigator 4.0 supports JavaScript 1.2.

Microsoft attempted to support JavaScript 1.0 in their Internet Explorer 3.0 browser. Known as "Jscript," Microsoft's initial JavaScript support was unreliable and buggy. A push to standardize the language resulted in an "official" version of JavaScript sanctioned by the ECMA. Internet Explorer 4.0 includes robust support for the ECMA standardized JavaScript, which, although it shares much in common with Netscape's JavaScript 1.2, is not exactly equivalent.

JavaScript code is typically embedded into an xHTML document using the SCRIPT tag. You are free to embed as many scripts into a single document as you like, using multiple SCRIPT tags. A script embedded in xHTML with the SCRIPT tag uses the format:

 
<script language="JavaScript">
<!--
document.write("Hello World!");
//-->
</script>

The LANGUAGE attribute is optional, but recommended. You may specify that a section of code only be executed by browsers which support a particular version of JavaScript; for instance:

 
<script language="JavaScript1.2">

Another attribute of the SCRIPT tag, SRC, can be used to include an external file containing JavaScript code rather than code embedded into the xHTML:

 
<script language="JavaScript" src="corefunctions.js">
</script>

The external file is simply a text file containing JavaScript code, and whose filename ends with the extension ".js". Note that although some version 3 browsers support the SRC attribute, it only functions reliably across platforms in the version 4 browsers.

Scripts can be placed inside comment fields to ensure that your JavaScript code is not displayed by older browsers that don't recognize JavaScript. The markup to begin a comment field is <!-- while you close a comment field using //-->. This practice is certainly optional, but considered good form when your page is likely to be visited by older browsers.

Examples

Alert boxes...

Alert:

How about a series of messages? Try this button. You'll get three messages in a row.

Enter a message in the box and check out how your alert message would look.

onMouseOver Examples: Put your cursor over the following link and watch the message that pops up:

Put your cursor here

Where do you want to go today?