reptile7's JavaScript blog
Saturday, June 11, 2005
Properties I: The Navigator Object
Blog Entry #12

In Blog Entry #2, we stated that JavaScript properties, as a part of speech, are the "adjectives" of JavaScript; we know now that they describe or modify JavaScript objects.

Some JavaScript properties are "read/write", i.e., we can both get (read) and set (write) their values; others are "read-only". Some JavaScript properties are highly useful; others are of marginal value.

Joe Burns' treatment of JavaScript properties in HTML Goodies' JavaScript Primers #7 leaves room for improvement. With respect to the four JavaScript objects that appear in the first six primers - document, Date, window, and Location - only document and Location properties are discussed in Primer #7. Disappointingly, there is no mention of the properties relating to text, image, and form elements.

Primer #7 also introduces two new objects, the JavaScript navigator and History objects, and discusses some of their properties.

HTML Goodies' JavaScript Properties Keyword Reference page is here. DevGuru's master list of JavaScript properties appears here.

Primer #7 begins with the navigator object, as will we. The navigator object is a host object that represents the user's browser; it was apparently named after the Netscape Navigator browser back in the day when Netscape was the dominant browser.

There is seemingly conflicting information on the Web as to where the navigator object fits in the JavaScript object hierarchy, which we'll discuss in Primer #8. Some Web sites put the navigator object at the top and/or outside of the hierarchy; others state that navigator is a property of the window object. My experience supports the latter view; the attempted execution of navigator.window commands (e.g., navigator.window.document.bgColor = "blue") on my computer gives only errors, but window.navigator.relevant_property operations all work OK.

The properties of the navigator object return information about the user's platform; these properties are all read-only. Primer #7 lists and defines four navigator properties: appName, appVersion, appCodeName, and userAgent. DevGuru's navigator object page lists these four plus four others; JavaScript Kit's navigator object page lists these four plus seven others.

Note that JavaScript Kit's navigator object page features a browser/operating system selection list ("Additional browsers' Navigator Information") whose individual options each pop up a display giving the relevant values of the basic navigator properties for that option. At first glance, much of this information seems suspect, for lack of a better word. For example, all of the listed browsers - all fifteen of them - return the same code name (appCodeName value), "Mozilla", which was the code name of the original Netscape browser. In addition, only one browser, Opera 7.5 (Identify as Opera), gives a return for the appVersion property - "7.50 (Windows NT 5.1; U)" - that one would reasonably expect. The browser that I use most often, Internet Explorer 5.1.6 for the Macintosh, gives as its version:

appVersion: 4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)

- needless to say, this is not what I would have anticipated. However, in many (but not all) cases, the browser version does appear in the string returned by the userAgent property. Lastly, even the seemingly straightforward appName property returns the name "Netscape" for the Firefox, Safari, Mozilla, and AOL browsers.

The main application of the navigator object seems to be its use in browser-detection scripts that determine the name and version of the user's browser and/or whether the user is using a PC or a Mac, and then either (a) sends the user to a page whose coding is specifically designed for the user's system, or more likely, (b) pops up a window with a "Sorry, but you cannot access the desired content because you do not meet the necessary system requirements" type of message. Two such scripts are discussed in the HTML Goodies JavaScript Script Tips; they respectively span Script Tips #10-12 and #13-15. Moreover, according to the Wikipedia references linked above, the reason why "Mozilla" appears in the identifying information of non-Netscape browsers is to prevent browser-detection scripts from rerouting users of those browsers away from designed-for-Netscape Web pages.

The user's platform information can be extracted from the returns of navigator properties using methods of the JavaScript String object - this is a can of worms that we can open when we look at the script tips cited above.

Of course, use of a browser-detection script implies that a Webmaster is creating specific or separate content for Netscape vs. MSIE, or for newer versions of a browser vs. older versions, or for a PC vs. a Mac, etc., to which I say, "Ugh". We would all be better off, IMHO, if we coded in the spirit of the Viewable with Any Browser campaign: "One World, One Internet, One Standard".

One World, One Internet, One Standard

Two of the more interesting navigator properties, plugins and mimeTypes, frustratingly violate the above concept; they are supported by Netscape for both Windows and Mac users, and are supported by MSIE 5+ for Mac users, but they are not supported by MSIE for Windows users (as noted here for the plugins property, and I am assuming the same is true for the mimeTypes property). Plugin and MimeType themselves are JavaScript objects, with their own properties; they can be used to determine if your browser can handle a given file or media type.

I'm a bit hesitant to illustrate the plugins and mimeTypes properties/objects because of their lack of general usability, but hey, why not? At the risk of getting seriously ahead of ourselves, the following code (a for loop, an array, oh my...) can be used to print out the plugins that your browser has at its disposal:

<script type="text/javascript">
document.write("What plugins does your browser use to extend its functionality?");
for (i = 0; i < navigator.plugins.length; i ++) {
document.write("<br>(" + i + ") " + navigator.plugins[i].name); }

The output on my computer when using MSIE 5.1.6 is:
What plugins does your browser use to extend its functionality?
(0) Default Plugin
(1) Digital Rights Management Plugin
(2) MRJ Java Plugin
(3) RealPlayer(tm) G2 LiveConnect-Enabled Plug-in (Mac)
(4) Shockwave Flash
(5) Windows Media Plugin
(6) QuickTime Plug-in 5.0.2

(Again, this script probably won't work for the MSIE/Windows folks, but the rest of you might give it a shot.)

As you can see from JavaScript Kit's navigator object page, the language-related properties of the navigator object - e.g., language, systemLanguage, and userLanguage - are also browser-specific. But at least the cookieEnabled property, which returns true if the browser is set to accept cookies and false if it isn't, is browser-independent.

That'll do it for the navigator object and its properties. Internet Related Technologies lists some additional navigator properties that are not covered above, but I think we're good to go for the time being. Next up: the properties of the document object, which we'll get into in the following post.


Comments: Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)