Cari di JavaScript 
    JavaScript Manual
Daftar Isi
(Sebelumnya) OptionChapter 9. Events and Event Ha ... (Berikutnya)

Chapter 8
Browser

This chapter deals with the browser and elements associated with it.

Table 8.1 summarizes the objects in this chapter.

Table 8.1 Browser-related objects

Object Description
navigator
Contains information about the version of Navigator in use.

MimeType
Represents a MIME type (Multipart Internet Mail Extension) supported by the client.

Plugin
Represents a plug-in module installed on the client.


navigator

Contains information about the version of Navigator in use.

Client-side object

Implemented in

Navigator 2.0
Navigator 3.0: added mimeTypes and plugins properties; added javaEnabled and taintEnabled methods.
Navigator 4.0: added language and platform properties; added preference method.

Created by

The JavaScript runtime engine on the client automatically creates the navigator object.

Description

Use the navigator object to determine which version of the Navigator your users have, what MIME types the user's Navigator can handle, and what plug-ins the user has installed. All of the properties of the navigator object are read-only.

Property Summary

appCodeName
Specifies the code name of the browser.

appName
Specifies the name of the browser.

appVersion
Specifies version information for the Navigator.

language
Indicates the translation of the Navigator being used.

mimeTypes
An array of all MIME types supported by the client.

platform
Indicates the machine type for which the Navigator was compiled.

plugins
An array of all plug-ins currently installed on the client.

userAgent
Specifies the user-agent header.

Method Summary

javaEnabled
Tests whether Java is enabled.

plugins.refresh
Makes newly installed plug-ins available and optionally reloads open documents that contain plug-ins.

preference
Allows a signed script to get and set certain Navigator preferences.

taintEnabled
Specifies whether data tainting is enabled.

Properties

appCodeName

A string specifying the code name of the browser.

Property of

navigator

Read-only

Implemented in

Navigator 2.0

Examples

The following example displays the value of the appCodeName property:

document.write("The value of navigator.appCodeName is " +
   navigator.appCodeName)
For Navigator 2.0 and 3.0, this displays the following:

The value of navigator.appCodeName is Mozilla

appName

A string specifying the name of the browser.

Property of

navigator

Read-only

Implemented in

Navigator 2.0

Examples

The following example displays the value of the appName property:

document.write("The value of navigator.appName is " +
   navigator.appName)
For Navigator 2.0 and 3.0, this displays the following:

The value of navigator.appName is Netscape

appVersion

A string specifying version information for the Navigator.

Property of

navigator

Read-only

Implemented in

Navigator 2.0

Description

The appVersion property specifies version information in the following format:

releaseNumber (platform; country)

The values contained in this format are the following:

  • releaseNumber is the version number of the Navigator. For example, "2.0b4" specifies Navigator 2.0, beta 4.
  • platform is the platform upon which the Navigator is running. For example, "Win16" specifies a 16-bit version of Windows such as Windows 3.1.
  • country is either "I" for the international release, or "U" for the domestic U.S. release. The domestic release has a stronger encryption feature than the international release.

Examples

Example 1. The following example displays version information for the Navigator:

document.write("The value of navigator.appVersion is " +
   navigator.appVersion)
For Navigator 2.0 on Windows 95, this displays the following:

The value of navigator.appVersion is 2.0 (Win95, I)
For Navigator 3.0 on Windows NT, this displays the following:

The value of navigator.appVersion is 3.0 (WinNT, I)
Example 2. The following example populates a Textarea object with newline characters separating each line. Because the newline character varies from platform to platform, the example tests the appVersion property to determine whether the user is running Windows (appVersion contains "Win" for all versions of Windows). If the user is running Windows, the newline character is set to ; otherwise, it's set to , which is the newline character for Unix and Macintosh.

<SCRIPT>
var newline=null
function populate(textareaObject){
   if (navigator.appVersion.lastIndexOf('Win') != -1)
      newline=""
      else newline=""
   textareaObject.value="line 1" + newline + "line 2" + newline
   + "line 3"
}
</SCRIPT>
<FORM NAME="form1">
<BR><TEXTAREA NAME="testLines" ROWS=8 COLS=55></TEXTAREA>
<P><INPUT TYPE="button" VALUE="Populate the Textarea object"
   onClick="populate(document.form1.testLines)">
</TEXTAREA>
</FORM>

language

Indicates the translation of the Navigator being used.

Property of

navigator

Read-only

Implemented in

Navigator 4.0

Description

The value for language is usually a 2-letter code, such as "en" and occasionally a five-character code to indicate a language subtype, such as "zh_CN".

Use this property to determine the language of the Navigator client software being used. For example you might want to display translated text for the user.

mimeTypes

An array of all MIME types supported by the client.

Property of

navigator

Read-only

Implemented in

Navigator 3.0

The mimeTypes array contains an entry for each MIME type supported by the client (either internally, via helper applications, or by plug-ins). For example, if a client supports three MIME types, these MIME types are reflected as navigator.mimeTypes[0], navigator.mimeTypes[1], and navigator.mimeTypes[2].

Each element of the mimeTypes array is a MimeType object.

See also

MimeType

platform

Indicates the machine type for which the Navigator was compiled.

Property of

navigator

Read-only

Implemented in

Navigator 4.0

Description

Platform values are Win32, Win16, Mac68k, MacPPC and various Unix.

The machine type the Navigator was compiled for may differ from the actual machine type due to version differences, emulators, or other reasons.

If you use SmartUpdate to download software to a user's machine, you can use this property to ensure that the trigger downloads the appropriate JAR files. The triggering page checks the Navigator version before checking the platform property. For information on using SmartUpdate, see Using JAR Installation Manager for SmartUpdate.

plugins

An array of all plug-ins currently installed on the client.

Property of

navigator

Read-only

Implemented in

Navigator 3.0

You can refer to the Plugin objects installed on the client by using this array. Each element of the plugins array is a Plugin object. For example, if three plug-ins are installed on the client, these plug-ins are reflected as navigator.plugins[0], navigator.plugins[1], and navigator.plugins[2].

To use the plugins array:

1. navigator.plugins[index]
2. navigator.plugins[index][mimeTypeIndex]
index is an integer representing a plug-in installed on the client or a string containing the name of a Plugin object (from the name property). The first form returns the Plugin object stored at the specified location in the plugins array. The second form returns the MimeType object at the specified index in that Plugin object.

To obtain the number of plug-ins installed on the client, use the length property: navigator.plugins.length.

plugins.refresh: The plugins array has its own method, refresh. This method makes newly installed plug-ins available, updates related arrays such as the plugins array, and optionally reloads open documents that contain plug-ins. You call this method with one of the following statements:

navigator.plugins.refresh(true)
navigator.plugins.refresh(false)
If you supply true, refresh refreshes the plugins array to make newly installed plug-ins available and reloads all open documents that contain embedded objects (EMBED tag). If you supply false, it refreshes the plugins array, but does not reload open documents.

When the user installs a plug-in, that plug-in is not available until refresh is called or the user closes and restarts Navigator.

Examples

The following code refreshes arrays and reloads open documents containing embedded objects:

navigator.plugins.refresh(true)
See also the examples for the Plugin object.

userAgent

A string representing the value of the user-agent header sent in the HTTP protocol from client to server.

Property of

navigator

Read-only

Implemented in

Navigator 2.0

Description

Servers use the value sent in the user-agent header to identify the client.

Examples

The following example displays userAgent information for the Navigator:

document.write("The value of navigator.userAgent is " +
   navigator.userAgent)
For Navigator 2.0, this displays the following:

The value of navigator.userAgent is Mozilla/2.0 (Win16; I)

Methods

javaEnabled

Tests whether Java is enabled.

Method of

navigator

Static

Implemented in

Navigator 3.0

Syntax

javaEnabled()

Parameters

None.

Description

javaEnabled returns true if Java is enabled; otherwise, false. The user can enable or disable Java by through user preferences.

Examples

The following code executes function1 if Java is enabled; otherwise, it executes function2.

if (navigator.javaEnabled()) {
   function1()
}
else function2()

See also

navigator.appCodeName, navigator.appName, navigator.userAgent

preference

Allows a signed script to get and set certain Navigator preferences.

Method of

navigator

Static

Implemented in

Navigator 4.0

Syntax

preference(prefName)
preference(prefName, setValue)

Parameters

prefName
A string representing the name of the preference you want to get or set. Allowed preferences are listed below.

setValue
The value you want to assign to the preference. This can be a string, number, or Boolean.

Description

This method returns the value of the preference. If you use the method to set the value, it returns the new value.

Security

Reading a preference with the preference method requires the UniversalPreferencesRead privilege. Setting a preference with this method requires the UniversalPreferencesWrite privilege.

For information on security in Navigator 4.0, see Chapter 7, "JavaScript Security," in the JavaScript Guide.

With permission, you can get and set the preferences shown in Table 8.2.

Table 8.2 Preferences.  

To do this...Set this preference...To...
Automatically load images

general.always_load_images
true or false

Enable Java

security.enable_java
true or false

Enable JavaScript

javascript.enabled
true or false

Enable style sheets

browser.enable_style_sheets
true or false

Enable SmartUpdate

autoupdate.enabled
true or false

Accept all cookies

network.cookie.cookieBehavior
0

Accept only cookies that get sent back to the originating server

network.cookie.cookieBehavior
1

Disable cookies

network.cookie.cookieBehavior
2

Warn before accepting cookie

network.cookie.warnAboutCookies
true or false

taintEnabled

Specifies whether data tainting is enabled.

Method of

navigator

Static

Implemented in

Navigator 3.0; removed in Navigator 4.0

Syntax

navigator.taintEnabled()

Description

Tainting prevents other scripts from passing information that should be secure and private, such as directory structures or user session history. JavaScript cannot pass tainted values on to any server without the end user's permission.

Use taintEnabled to determine if data tainting is enabled. taintEnabled returns true if data tainting is enabled, false otherwise. The user enables or disables data tainting by using the environment variable NS_ENABLE_TAINT.

Examples

The following code executes function1 if data tainting is enabled; otherwise it executes function2.

if (navigator.taintEnabled()) {
   function1()
   }
else function2()

See also

taint, untaint


MimeType

A MIME type (Multipart Internet Mail Extension) supported by the client.

Client-side object

Implemented in

Navigator 3.0

Created by

You do not create MimeType objects yourself. These objects are predefined JavaScript objects that you access through the mimeTypes array of the navigator or Plugin object:

navigator.mimeTypes[index]
where index is either an integer representing a MIME type supported by the client or a string containing the type of a MimeType object (from the MimeType.type property).

Description

Each MimeType object is an element in a mimeTypes array. The mimeTypes array is a property of both navigator and Plugin objects. For example, the following table summarizes the values for displaying JPEG images:

ExpressionValue
navigator.mimeTypes["image/jpeg"].type

image/jpeg

navigator.mimeTypes["image/jpeg"].description

JPEG Image

navigator.mimeTypes["image/jpeg"].suffixes

jpeg, jpg, jpe, jfif, pjpeg, pjp

navigator.mimeTypes["image/jpeg"].enabledPlugins

null

Property Summary

description
A description of the MIME type.

enabledPlugin
Reference to the Plugin object configured for the MIME type.

suffixes
A string listing possible filename extensions for the MIME type, for example "mpeg, mpg, mpe, mpv, vbs, mpegv".

type
The name of the MIME type, for example "video/mpeg" or "audio/x-wav".

Methods

None.

Examples

The following code displays the type, description, suffixes, and enabledPlugin properties for each MimeType object on a client:

document.writeln("<TABLE BORDER=1><TR VALIGN=TOP>",
   "<TH ALIGN=left>i",
   "<TH ALIGN=left>type",
   "<TH ALIGN=left>description",
   "<TH ALIGN=left>suffixes",
   "<TH ALIGN=left>enabledPlugin.name</TR>")
for (i=0; i < navigator.mimeTypes.length; i++) {
   document.writeln("<TR VALIGN=TOP><TD>",i,
      "<TD>",navigator.mimeTypes[i].type,
      "<TD>",navigator.mimeTypes[i].description,
      "<TD>",navigator.mimeTypes[i].suffixes)
   if (navigator.mimeTypes[i].enabledPlugin==null) {
      document.writeln(
      "<TD>None",
      "</TR>")
   } else {
      document.writeln(
      "<TD>",navigator.mimeTypes[i].enabledPlugin.name,
      "</TR>")
   }
}
document.writeln("</TABLE>")
The preceding example displays output similar to the following:

i type description suffixes enabledPlugin.name
0

audio/aiff

AIFF

aif, aiff

LiveAudio

1

audio/wav

WAV

wav

LiveAudio

2

audio/x-midi

MIDI

mid, midi

LiveAudio

3

audio/midi

MIDI

mid, midi

LiveAudio

4

video/msvideo

Video for Windows

avi

NPAVI32 Dynamic Link Library

5

*

Netscape Default Plugin

Netscape Default Plugin

6

zz-application/zz-winassoc-TGZ

TGZ

None

See also

navigator, navigator.mimeTypes, Plugin

Properties

description

A human-readable description of the data type described by the MIME type object.

Property of

MimeType

Read-only

Implemented in

Navigator 3.0

enabledPlugin

The Plugin object for the plug-in that is configured for the specified MIME type If the MIME type does not have a plug-in configured, enabledPlugin is null.

Property of

MimeType

Read-only

Implemented in

Navigator 3.0

Description

Use the enabledPlugin property to determine which plug-in is configured for a specific MIME type. Each plug-in may support multiple MIME types, and each MIME type could potentially be supported by multiple plug-ins. However, only one plug-in can be configured for a MIME type. (On Macintosh and Unix, the user can configure the handler for each MIME type; on Windows, the handler is determined at browser start-up time.)

The enabledPlugin property is a reference to a Plugin object that represents the plug-in that is configured for the specified MIME type.

You might need to know which plug-in is configured for a MIME type, for example, to dynamically emit an EMBED tag on the page if the user has a plug-in configured for the MIME type.

Examples

The following example determines whether the Shockwave plug-in is installed. If it is, a movie is displayed.

// Can we display Shockwave movies?
mimetype = navigator.mimeTypes["application/x-director"]
if (mimetype) {
   // Yes, so can we display with a plug-in?
   plugin = mimetype.enabledPlugin
   if (plugin)
      // Yes, so show the data in-line
      document.writeln("Here's a movie: <EMBED SRC=mymovie.dir HEIGHT=100 WIDTH=100>")
      else
      // No, so provide a link to the data
      document.writeln("<A HREF='mymovie.dir'>Click here</A> to see a movie.")
   } else {
   // No, so tell them so
   document.writeln("Sorry, can't show you this cool movie.")
}

suffixes

A string listing possible file suffixes (also known as filename extensions) for the MIME type.

Property of

MimeType

Read-only

Implemented in

Navigator 3.0

Description

The suffixes property is a string consisting of each valid suffix (typically three letters long) separated by commas. For example, the suffixes for the "audio/x-midi" MIME type are "mid, midi".

type

A string specifying the name of the MIME type. This string distinguishes the MIME type from all others; for example "video/mpeg" or "audio/x-wav".

Property of

MimeType

Read-only

Implemented in

Navigator 3.0

Property of

MimeType


Plugin

A plug-in module installed on the client.

Client-side object

Implemented in

Navigator 3.0

Created by

Plugin objects are predefined JavaScript objects that you access through the navigator.plugins array.

Description

A Plugin object is a plug-in installed on the client. A plug-in is a software module that the browser can invoke to display specialized types of embedded data within the browser. The user can obtain a list of installed plug-ins by choosing About Plug-ins from the Help menu.

Each Plugin object is itself array containing one element for each MIME type supported by the plug-in. Each element of the array is a MimeType object. For example, the following code displays the type and description properties of the first Plugin object's first MimeType object.

myPlugin=navigator.plugins[0]
myMimeType=myPlugin[0]
document.writeln('myMimeType.type is ',myMimeType.type,"<BR>")
document.writeln('myMimeType.description is ',myMimeType.description)
The preceding code displays output similar to the following:

myMimeType.type is video/quicktime
myMimeType.description is QuickTime for Windows
The Plugin object lets you dynamically determine which plug-ins are installed on the client. You can write scripts to display embedded plug-in data if the appropriate plug-in is installed, or display some alternative information such as images or text if not.

Plug-ins can be platform dependent and configurable, so a Plugin object's array of MimeType objects can vary from platform to platform, and from user to user.

Each Plugin object is an element in the plugins array.

When you use the EMBED tag to generate output from a plug-in application, you are not creating a Plugin object. Use the document.embeds array to refer to plug-in instances created with EMBED tags. See the document.embeds array.

Property Summary

description
A description of the plug-in.

filename
Name of the plug-in file on disk.

length
Number of elements in the plug-in's array of MimeType objects.

name
Name of the plug-in.

Examples

Example 1. The user can obtain a list of installed plug-ins by choosing About Plug-ins from the Help menu. To see the code the browser uses for this report, choose About Plug-ins from the Help menu, then choose Page Source from the View menu.

Example 2. The following code assigns shorthand variables for the predefined LiveAudio properties.

var myPluginName = navigator.plugins["LiveAudio"].name
var myPluginFile = navigator.plugins["LiveAudio"].filename
var myPluginDesc = navigator.plugins["LiveAudio"].description
Example 3. The following code displays the message "LiveAudio is configured for audio/wav" if the LiveAudio plug-in is installed and is enabled for the "audio/wav" MIME type:

var myPlugin = navigator.plugins["LiveAudio"]
var myType = myPlugin["audio/wav"]
if (myType && myType.enabledPlugin == myPlugin)
   document.writeln("LiveAudio is configured for audio/wav")
Example 4. The following expression represents the number of MIME types that Shockwave can display:

navigator.plugins["Shockwave"].length
Example 5. The following code displays the name, filename, description, and length properties for each Plugin object on a client:

document.writeln("<TABLE BORDER=1><TR VALIGN=TOP>",
   "<TH ALIGN=left>i",
   "<TH ALIGN=left>name",
   "<TH ALIGN=left>filename",
   "<TH ALIGN=left>description",
   "<TH ALIGN=left># of types</TR>")
for (i=0; i < navigator.plugins.length; i++) {
   document.writeln("<TR VALIGN=TOP><TD>",i,
      "<TD>",navigator.plugins[i].name,
      "<TD>",navigator.plugins[i].filename,
      "<TD>",navigator.plugins[i].description,
      "<TD>",navigator.plugins[i].length,
      "</TR>")
}
document.writeln("</TABLE>")
The preceding example displays output similar to the following:

i

name

filename

description

# of types

0

QuickTime Plug-In

d:ettoolsetscapeav30Program
pluginsNPQTW32.DLL

QuickTime Plug-In for Win32 v.1.0.0

1

1

LiveAudio

d:ettoolsetscapeav30Program
pluginsNPAUDIO.DLL

LiveAudio - Netscape Navigator sound playing component

7

2

NPAVI32 Dynamic Link Library

d:ettoolsetscapeav30Program
pluginspavi32.dll

NPAVI32, avi plugin DLL

2

3

Netscape Default Plugin

d:ettoolsetscapeav30Program
pluginspnul32.dll

Null Plugin

1

See also

MimeType, document.embeds

Properties

description

A human-readable description of the plug-in. The text is provided by the plug-in developers.

Property of

Plugin

Read-only

Implemented in

Navigator 3.0

filename

The name of a plug-in file on disk.

Property of

Plugin

Read-only

Implemented in

Navigator 3.0

Description

The filename property is the plug-in program's file name and is supplied by the plug-in itself. This name may vary from platform to platform.

Examples

See the examples for Plugin.

length

The number of elements in the plug-in's array of MimeType objects.

Property of

Plugin

Read-only

Implemented in

Navigator 3.0

name

A string specifying the plug-in's name.

Property of

Plugin

Read-only

Implemented in

Navigator 3.0

Security

Navigator 3.0: This property is tainted by default. For information on data tainting, see "JavaScript Security".

Description

The plug-in's name, supplied by the plug-in itself. Each plug-in should have a name that uniquely identifies it.

(Sebelumnya) OptionChapter 9. Events and Event Ha ... (Berikutnya)