Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Browsers and JavaScript

Like HTML, JavaScript requires a Web browser to be displayed, and different browsers may display it differently. Unlike HTML, the results of a browser incompatibility with JavaScript are more drastic: Rather than simply displaying your text incorrectly, the script may not execute at all, may display an error message, or may even crash the browser.

We'll take a quick look at the way different browsers—and different versions of the same browser—treat JavaScript in the following sections.

Netscape and Internet Explorer

Today's Web is dominated by two popular Web browsers: Netscape and Microsoft Internet Explorer. Netscape spent a few years being the more popular browser, but is now behind Internet Explorer in popularity; however, both are common enough that you should try to support them with your pages.

Versions of JavaScript

The JavaScript language has evolved since its original release in Netscape 2.0. There have been several versions of JavaScript:

Each of these versions is an improvement over the previous version and includes a number of new features. With rare exception, browsers that support the new version will also support scripts written for earlier versions.

ECMA, the European Computer Manufacturing Association, has finalized the ECMA-262 specification for ECMAScript, a standardized version of JavaScript. JavaScript 1.3 follows the ECMA-262 standard, and JavaScript 1.5 follows ECMA-262 revision 3.

Netscape is also working with ECMA on JavaScript 2.0, a future version that will correspond with the fourth edition of the ECMAScript standard. JavaScript 2.0 will improve upon earlier versions with a more modular approach, better object support, and features to make JavaScript useful as a general-purpose scripting language as well as a Web language.

Specifying JavaScript Versions

As mentioned earlier in this hour, you can specify a version of JavaScript in the <script> tag. For example, this tag specifies JavaScript version 1.3:

<script language="JavaScript1.3" type="text/javascript1.3">

There are two ways of specifying the version number. The old method uses the language attribute, and the new method recommended by the HTML 4.0 specification uses the type attribute. To maintain compatibility with older browsers, you can use both attributes.

When you specify a version number, this allows your script to execute only if the browser supports the version you specified or a later version.

When the <script> tag doesn't specify a version number, the browser assumes that the script is compatible with JavaScript 1.0. Since the core of the JavaScript language is unchanged since version 1.0, this is fine for most scripts.

JavaScript Beyond the Browser

While JavaScript programs traditionally run within a Web browser, and Web-based JavaScript is the focus of this book, JavaScript is becoming increasingly popular in other applications. Here are a few examples:

Along with these examples, many of the changes in the upcoming JavaScript 2.0 are designed to make it more suitable as a general-purpose scripting language.

Share ThisShare This

Informit Network