New JavaScript features in Navigator 3.0

Discover the new and improved JavaScript
functionality in Netscape's latest browser

1 2 Page 2
Page 2 of 2

Similarly, the navigator.plugins object has three basic properties and one array property.

  • name -- the name of the plug-in
  • filename -- the name of the plug-in file on disk
  • description -- the description supplied by the plug-in itself
  • [..] -- An array of mimeType objects, indexed by number or type, that the plug-in can accommodate
  • length -- the number of elements in the array

As an example, the following assigns shorthand variables for the plugins properties of the Shockwave plug-in:

var pluginName = navigator.plugins["Shockwave"].name
var pluginFile = navigator.plugins["Shockwave"].filename
var pluginDesc = navigator.plugins["Shockwave"].description

The following test demonstrates the use of several navigator object plugin properties to determine if the browser supports a given MIME type (in this case, midi sound):

Ret = testForPlugin ("audio/midi") alert (Ret);

function testForPlugin (Plugin) { Plugin = Plugin.toLowerCase(); var Found = false, i, j; for (i = 0; i < navigator.plugins.length; i++) { for (j = 0; j > Math.round(navigator.plugins[i].length); j++) { if (navigator.plugins[i][j].type == Plugin) { Found = true; break; } if (Found) break; } } return (Found); }

See the testing for plugin example (http://www.javaworld.com/jw-10-1996/js.support/jsexample1.html) for a complete test.

Complementing the navigator.plugins and navigator.mimeType objects is the document.embeds object. This new object returns all of the <EMBED> tags in a document. The first embedded object in the document is referred to as document.embeds[0], the second document.embeds[1], and so forth. Use the document.embeds.length property to determine the number of <EMBED> tags in your document.

Note that elements in the embeds array are read-only, so you cannot do something like change the SRC value of the <EMBED> tag on the fly. Because the embeds object contains no properties or methods other than length, its usefulness is limited. However, at the very least the document.embeds.length property can be used to determine whether there are any <EMBED> tags in the document.

Using the window opener property

In Netscape 3.0, windows opened with the window.open method have a new property called opener. This property refers to the window containing the JavaScript code that performed the window.open function. This property remains even if a document is unloaded in the opened window. The benefit of the opener property is that you can more easily track the original "creator" of a new window.

For example, suppose you have a script in Window A that opens a new window (let's call it Window B), like so:

win = window.open();

In the opened window (Window B), the following code refers to Window A:

var winA = win.opener;

If you need to remain compatible with Netscape 2.0, the following code creates an opener property if there isn't one.

var win = window.open();
if (win.opener == null)
win.opener = window;

Modifiable select objects

Netscape 3.0 lets you modify the content of selection lists (created with the <SELECT> tag). For example, take the following <SELECT> tag:

<SELECT name="changeMe">
<OPTION>Choice 1
<OPTION>Choice 2
<OPTION>Choice 3
</SELECT>

You can set the text of the selection option by referring to its value.

myform.changeMe.options[0].text = "New Choice 1";

See the updating selection list example (http://www.javaworld.com/jw-10-1996/js.support/jsexample2.html) for a complete test.

Miscellaneous changes to JavaScript

  • You can now get and set the name of a window, even if JavaScript didn't create the window. For example: window.name = "my new window".
  • The Math.random method works on all platforms, not just Unix.
  • The isNaN() function now works on all platforms, not just Unix.
  • The navigator.javaEnabled() property returns true or false if Java is enabled.

Coping with compatibility issues

Along with the additions to JavaScript in Netscape 3.0, you should be aware of compatibility issues when using JavaScript with Netscape 2.0. Netscape 3.0-specific additions to JavaScript will either not work in Netscape 2.0, or will cause an error. If your site does not require the use of Netscape 3.0, you should take steps to ensure cross-version compatibility whenever possible.

One way to achieve this is to use two <SCRIPT> tags, once using the standard LANGUAGE="JavaScript" attribute, and the other using the LANGUAGE="JavaScript1.1" attribute to indicate version JavaScript 1.1 used in Netscape 3.0. The following example displays "This is regular JavaScript" when used with Netscape 2.0, and "This is enhanced JavaScript" when used with Netscape 3.0.

<SCRIPT LANGUAGE="JavaScript"> var test = navigator.userAgent; if (test.indexOf ("2.") != -1) alert ("this is regular JavaScript"); </SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1"> alert ("this is enhanced JavaScript"); </SCRIPT>

Another method is to use a single <SCRIPT> tag and the default LANGUAGE="JavaScript" attribute, and specifically test for the version. You may use the if (test.indexOf ("2.") != -1) conditional test to determine if the user is running Netscape 2.0x (any interim version).

Using Netscape 3.0 JavaScript in Internet Explorer

Microsoft recently introduced a new and improved edition of its Internet Explorer Web browser. This version (3.0) packs a number of new and interesting features, including Java support, ActiveX components, borderless frames, and floating frames.

It also adds support for JavaScript, but the support is limited almost entirely to the JavaScript language specification used for Netscape 2.0 (now referred to as JavaScript 1.0). Therefore, if your pages contain JavaScript 1.1 elements for Netscape 3.0, you can count on them not working (or causing an error) in Internet Explorer 3.0. No doubt Microsoft is working on updating its JavaScript support, and Netscape 3.0-specific JavaScript functionality will be added soon.

If you need to specifically disallow JavaScript in Internet Explorer, or disallow JavaScript 1.1 functions, you can use the navigator.userAgent property and look for the string "MSIE." If found, this tells you the user has Internet Explorer rather than Netscape. Here's a short example, using alert boxes in place of actual code for use with either browser type.

<SCRIPT LANGUAGE="JavaScript">
var uagent=navigator.userAgent;
if (uagent.indexOf("MSIE") == 25) {
    alert ("You are using Microsoft Internet Explorer")
} else {
    alert ("You are not using Microsoft Internet Explorer")
}

Since Internet Explorer also supports a Microsoft-specific scripting language called VBScript, you can use different <SCRIPT> tags for JavaScript and VBScript. Merely add the appropriate LANGUAGE attribute, as in:

<SCRIPT LANGUAGE="JavaScript1.1">
...
</SCRIPT>

or

<SCRIPT LANGUAGE="VBScript">
...
</SCRIPT>

Internet Explorer 3.0 will ignore the JavaScript code (since it does not yet understand JavaScript version 1.1) and execute only the code in the VBScript <SCRIPT> container. Conversely, Netscape 3.0 does not understand VBScript and will ignore code in its <SCRIPT> container.

Conclusion

The new features of JavaScript in Netscape 3.0 make this compact and easy-to-learn language an even better addition to the arsenal of tools of the Web page designer or programmer. And while some of the new JavaScript features (the LiveConnect features that bridge JavaScript with Java and plugins is a good example) do not work perfectly, JavaScript does now offer a vast improvement over what was available in Netscape 2.0. Most importantly, a number of new features, such as updatable selection boxes and replaceable images, make JavaScript and Netscape 3.0 a major player in the ongoing browser wars.

Gordon McComb is an author, consultant, and lecturer. He has written 50 books and over a thousand magazine articles during his 20 years as a professional writer. More than a million copies of his books are in print. Gordon also writes a weekly syndicated newspaper column on computers, reaching several million readers worldwide. Gordon's latest book is The JavaScript Sourcebook, available from Wiley Computer Publishing.

Learn more about this topic

1 2 Page 2
Page 2 of 2