|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Reading Browser InformationIn Hour 4, "Working with the Document Object Model (DOM)," you learned about the various objects (such as window and document) that represent portions of the browser window and the current web document. JavaScript also includes an object called navigator that you can use to read information about the user's browser. The navigator object isn't part of the DOM, so you can refer to it directly. It includes a number of properties, each of which tells you something about the browser. These include the following:
By the Way As you might have guessed, the navigator object is named after Netscape Navigator, the browser that originally supported JavaScript. Fortunately, this object is also supported by Internet Explorer and most other recent browsers. Displaying Browser InformationAs an example of how to read the navigator object's properties, Listing 15.1 shows a script that displays a list of the properties and their values for the current browser. Listing 15.1. A Script to Display Information About the Browser
This script includes a basic HTML document. A script is used within the body of the document to display each of the properties of the navigator object using the document.write() statement. To try this script, load it into the browser of your choice. If you have more than one browser or browser version handy, try it in each one. Firefox's display of the script is shown in Figure 15.1. Figure 15.1. Firefox displays the browser information script.
Dealing with Dishonest BrowsersIf you tried the browser information script in Listing 15.1 using one of the latest versions of Internet Explorer, you probably got a surprise. Figure 15.2 shows how Internet Explorer 6.0 displays the script. Figure 15.2. Internet Explorer displays the browser information script.
There are several unexpected things about this display. First of all, the navigator.language property is listed as undefined. This isn't much of a surprise because this property isn't yet supported by Internet Explorer. More important, you'll notice that the word Mozilla appears in the Code Name and User Agent fields. The full user agent string reads as follows: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Believe it or not, Microsoft did have a good reason for this. At the height of the browser wars, about the time Netscape 3.0 and IE 3.0 came out, it was becoming common to see "Netscape only" pages. Some webmasters who used features such as frames and JavaScript set their servers to turn away browsers without Mozilla in their user agent string. The problem with this was that most of these features were also supported by Internet Explorer. Microsoft solved this problem in IE 4.0 by making IE's user agent read Mozilla, with the word compatible in parentheses. This allows IE users to view those pages, but still includes enough details to tell web servers which browser is in use. You've probably already noticed the other problem with Internet Explorer 6.0's user agent string: the portion reading Mozilla/4.0. Not only is IE claiming to be Netscape, but it's also masquerading as version 4.0. Why? As it turns out, this was another effort by Microsoft to stay one step ahead of the browser wars, although this one doesn't make quite as much sense. Because poorly written scripts were checking specifically for "Mozilla/4" for dynamic HTML pages, Microsoft was concerned that its 5.0 version would fail to run these pages. Since changing it now would only create more confusion, this tradition continues with IE 6.0. By the Way Microsoft isn't alone in confusing browser IDs. Netscape version 6 displays a user agent string beginning with Mozilla/5, and an app version of 5.0. (Netscape 5.0 was Netscape's open-source browser, code named Mozilla, which formed the foundation of Netscape 6 and Firefox.) Although these are two interesting episodes in the annals of the browser wars, what does all this mean to you? Well, you'll need to be careful when your scripts are trying to differentiate between IE and Netscape, and between different versions. You'll need to check for specific combinations instead of merely checking the navigator.appVersion value. Fortunately, there's a better way to handle this, as you'll learn in the next section. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |