In This Chapter
Back when I was a nervous youth, our board of education, possibly motivated by revenge, felt that it was necessary to provide three strata of physical education classes. Each strata was oriented towards a different Phys. Ed. capacity: budding professional, middling-but-mobile, and victim. Several weeks into the semester, we’d be run through a battery of quasi-legal “tests,” several of which probably violated the Geneva Convention. Result cards in hand, we’d stand in queue as the gym instructor (officially, “herr gym instructor”) looked us over and pointed us towards a collection point for one of the three strata.
Fortunately, those extended years of trauma now yield a poignant analogy: the Navigator Object, which identifies the Web browser being used to load the Web page, can be used to tailor a Web site towards a particular browser. It enables you to detect the presence of certain plug-ins or redirect users to new pages created for their browser’s capabilities.
What’s in a Name?
Understanding and using the Navigator Object is fairly simple. So there’s no point in “prevaricating around the bush.” Let’s begin!
The “codename” is the internal name by which the Web browser identifies itself. Netscape Navigator, for instance, identifies itself as Mozilla. (Curiously, Microsoft Internet Explorer 3.0 also identifies itself as Mozilla; this is a compatibility trick, which allows some pages and servers to offer the same content to Internet Explorer as it would to Netscape Navigator.)
will yield “Mozilla” if the visitor is using either Netscape Navigator or Microsoft Internet Explorer.
As opposed to the browser’s internal identification, this property reflects the external name of the browser. Thus, this expression:
yields “Netscape” when using Netscape Navigator, or yields “Microsoft Internet Explorer” when using said browser. We’ll use this property later to determine which browser a visitor is using and to redirect him to a suitable page.
This property yields the version number and platform of the browser. In some cases, such as with Internet Explorer, it also yields the Netscape version with which it is compatible. Thus, if the user is using the 32-bit Netscape Navigator 3.01 under Windows 95, this expression:
yields “3.01 (Win95; I).” And if the user is using Microsoft Internet Explorer 3.01 under Windows 95, the above yields “2.0 (compatible; MSIE 3.01; Windows 95).” This is Internet Explorer’s way of saying “I’m just like Netscape 2, although I’m actually MSIE 3.01.”
Technically, the userAgent is what the browser sends to the Web server as a form of identity. For our purposes, though, this property is basically a concatenation of appCodename and appVersion. This certainly doesn’t make it useless; if you want to parse the browser identity for some reference, you can use the userAgent property to get an all-in-one string. For instance, a visitor using 32-bit Netscape Navigator 3.01 under Windows 95 would generate a value of “Mozilla/3.01 (Win95; I)” if you checked the value of navigator.userAgent.
Who Goes Where?
This method returns a value of true if the user’s browser has Java capabilities enabled; otherwise it returns false. Using an expression such as this:
you can execute a Java applet only if the user has Java enabled. Alternatively, if Java is disabled, perhaps you could write a message alerting the user to enable Java to fully experience this page.
This array, named plugins, contains information on each plug-in installed for the browser (such as Shockwave, Quicktime, and so on). In using it most simply, you can check for the presence of an installed plug-in as shown here:
Using the above code snippet, you can tailor the page’s behavior to the presence or absence of a particular plug-in.
Also a part of the Navigator Object, mimeTypes is an array containing each of the mime types configured for the user’s browser. Mime types are types of files as configured either for plugins or for Helper Apps in the browser. For instance, a commonly configured mime type is “image/jpeg,” which then calls a defined jpeg image viewer.
To see this in action, you could verify the existence of a configured image/jpeg mime type with this code:
He Went That-a-Way
Using some of the knowledge in this chapter, you can use the Navigator Object to instantly and automatically redirect a visitor to the page designed for his browser.
Imagine, for instance, that you’re a very industrious sort. You’ve designed two versions of your home page: one with layout optimized for Netscape Navigator and one optimized for Microsoft Internet Explorer. For example, Internet Explorer 3.0 supports graphic backgrounds within table cells, but Navigator 3.0 does not. You’ve saved your two home pages as nspage.htm and msiepage.htm, respectively.
That’s all! In the above, you merely check for the string “Explorer” anywhere in the appName property of the Navigator Object. If it’s not present (–1), the user is not using Internet Explorer, and he is sent to the Netscape page (url = “nspage.htm”). (Of course, he may not be using Navigator, either, but that is your only alternative page.)
On the other hand, if the string “Explorer” is found within the appName property, the string indexOf() method returns its position (anything other than –1), and you know that the user is browsing with Internet Explorer. Thus, he is sent to msiepage.htm.
Similarly, you can use any of the Navigator Object properties and methods discussed in this chapter to tailor the behavior of a Web page.
The Least You Need to Know
For comments or technical support for our books and software, select Talk to Us. © 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.
To order books, call us at 800-716-0044 or 317-228-4366.
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.