Home

Help

Search

D-I-Y browser checker

In the second installment of their Javascript series, Helen Bradley and John Hilvert explain how to check the browsers of visitors to your Web site.

 

When you're using JavaScript on your web site you should make allowances for browsers that do not support JavaScript or which provide incomplete support.

This month's project shows how to find out details about your user's browser and to load different pages depending on which one they're using. This is a procedure you can use not only for your JavaScript routines but if one browser supports certain style sheet functions, frames or Dynamic HTML then you can provide different pages depending on the browser being used. This will also be useful where a user has a JavaScript enabled browser but has JavaScript support disabled.

The HTML source in this project has been written so that you can use it for a number of applications. It displays one of a range of 3 pages depending on what browser your user is using and is easily expandable beyond this. The source has been designed so that you can easily adapt it to your own programs.

 

FIG1Ds.gif (4222 bytes)
Figure 1d: This page is loaded by a JavaScript routine that loads designated page depending on the name and version number of the browser being used.
Creating the browser checker

We've provided all the files you need for this project on this month's cover CD-ROM, starting with the files browser.htm, page1.htm and page2.htm, which you will find in the \interact\diycheck folder on your CD-ROM drive.

Open browser.htm now. If you are using the PC User Offline on-disc browser, page1.htm will be displayed, because this is Internet Explorer 3 for Windows 3 which is not Javascript enabled. However, open browser.htm with Internet Explorer 4 or Netscape Navigator 3 or 4 (using the File, Open menu), and it will display page2.htm because these browsers are Javascript enabled. See figure 1d. IE 4.01, Navigator 4.04 and Communicator 4.04 are also on this month's cover disc in the Online Tools section.

To view or use the source code for each file, you can use any HTML editor, text editor and most Web browsers (the relevant command in your browser should be in the View menu).

 

FIG2Ds.gif (3830 bytes)
Figure 2d: If your browser is not IE4 or Netscape 3 or 4 or has JavaScript disabled (as Netscape has here) you will see the text from the BODY of the html document.
How it works

Details about the current browser being used are stored in various properties of the navigator object, see Javascript jargon box. The JavaScript here reads two of these navigator properties appVersion and appName and stores the information from them in two variables; browserVersion and browserName. It then looks at the contents of these variables to make a determination as to which browser is being used.

The first test is whether the browser is Internet Explorer. The substring method is used to make the test and it returns a portion of the string starting at position 0 in the string (the first character) and not including the character at position 27:

if (browserName.substring(0,27) == "Microsoft Internet Explorer")

If the browser is Internet Explorer then a test is made as to whether it is version 4. From the browserVersion variable, only the first character is needed to actually identify the version, for example 4.0 and 4.01 would be both 'version 4'. So the charAt method is used to extract the first character from the variable browserVersion ie. the character at position 0:

if (browserVersion.charAt(0) == 4)

If the browser is IE4 then the variable pageOption is assigned the value 2. This value will be used later to control which page is displayed on the screen.

If the browser was not IE4 then it tested in a similar way to see if it is Netscape and, if so whether it's version 3 or 4 which, together with IE4, both support JavaScript. If the browser is Netscape 3 the pageOption variable is assigned the value 1 and if it is Netscape 4 the pageOption variable is assigned the value 2.

The distinction drawn here between the two versions of Netscape is purely for demonstration purposes. However you could use it in a situation where a page contains a function which Netscape version 4 supports and version 3 doesn't.

Finally, on the basis of the value of the variable pageOption, a page is loaded. If the value of pageOption is 1 then the page page1.htm is loaded into the current window:

if (pageOption == 1)

self.location="page1.htm";

As soon as this line of code is encountered the browser will cease loading the current page and will locate and load this other page instead.

The 'self' property refers to the current window and the location object is set to the required html file, which must reside in the same directory as the file browser.htm. If it does not, then you will need to provide more detailed path information in the command.

If the browser is not IE4, Netscape 3 or 4 then the page will continue to load and the text in the BODY of the HTML will be displayed on the screen.

The logic of the JavaScript code is this:

 

if the browser is IE4 or Netscape 3 or 4

take the viewer to another page depending on which of these browsers it is

else if it is any other browser

do nothing

The actual effect of this is that any JavaScript enabled browser (other than IE4 and Netscape 3 or 4) will simply display the text in the BODY of the document because there are no instructions in the code providing for anything else to happen.

Any non-JavaScript-enabled browser will simply ignore the JavaScript code because it appears in comments in the HEAD of the document and it will display the text in the BODY of the document too. See figure 2d.

 

More JavaScript jargon explained
Navigator object. The navigator object is the browser that the user is currently using and it contains details about this browser. The navigator object has no methods or events but it does have some read only properties:

appCodeName returns a string with the code name of the browser. Netscape has the code name Mozilla, for example.

appName returns a string containing the name of the browser, for example Netscape or Microsoft Internet Explorer.

appVersion returns a string with the version number of the browser. Included in this string is the release number of the product, the platform it is being used on, a code indicating the country and often other information as well. The information which makes up the value of this property varies between browsers, see figure 3d.

userAgent returns a string which contains a value used by servers to identify the client. The result is similar to the combined information contained in the appName and appVersion properties.

Back button issues

Because this browser checker loads two pages with the first one refreshing the second, your browser's Back key won't operate as usual. Instead of taking you back from page1.htm (or page2.htm) to browser.htm it will take you back to the page prior to the browser checker page in your History list.

 

Customise it

This browser checker can be used with any of your Web pages. Remember, when using our code for your own purposes to pay particular attention to the capitalisation of the JavaScript code, otherwise you run the risk of encountering errors.

You only need change the value assigned to the pageOption variable and the html document to be associated with that value and you can send your viewer to any other page.

For example, to send the viewer to page3.htm, if they're using Netscape 4, alter these lines of code:

else if(browserVersion.charAt(0) == 4)

pageOption = 2;

so that the pageOption variable is set to 3, and add another if test:

else if (pageOption == 3)

self.location="page3.htm";

You may have noticed that there is one case that has not been specifically provided for in this example, but which you may want to include instructions to deal with. This is the case of a browser that is neither IE4 nor Netscape 3 or 4 but which supports JavaScript.

In this example it will be treated like any non JavaScript enabled browser or one with JavaScript disabled however you could control it differently. You can do this using the pageOption variable. In the program it was initialised with the value 0 in the command:

pageOption = 0

Any browser that is JavaScript enabled but is not IE4 or either Netscape 3 or 4 will retain the value 0 in the variable pageOption and can be controlled with the command:

if (pageOption == 0)

self.location = place page reference in here;

You can see for yourself how this works by opening the file browser2.htm in the /interact/diycheck folder of this month's CD. If your browser is Netscape Navigator 4 or Communicator 4, then it will display page3.htm. You can view or use the source code for the file with any HTML editor, text editor and most Web browsers (the relevant command in your browser is in the View menu).

You can add functionality to your site for users who aren't able to view your JavaScript pages by including in the BODY of your browser checking page a note to your user that you suggest they upgrade their browser to fully appreciate your site. You can include a hyperlink to Microsoft's IE 4 or Netscape's Navigator site.

 

FIG3Ds.gif (1873 bytes)
Figure 3d: Using the file sample1.htm you can find out information about your browser.
Incorporate last month's clock

Last month's JavaScript clock works with IE4 and Navigator 3 and 4. You can use the code in this month's browser check to display the clock only if a user's browser is one of the ones that it will run in. We provided the file clock.htm in the /interact/diycheck folder of this month's CD, plus the file jsclock.htm from our first Javascript tutorial needed to run clock.htm. You can view or use the source code for each file with any HTML editor, text editor and most Web browsers.

 

Investigating the Navigator properties

Also on the cover disc is a file called sample1.htm in the /interact/diycheck folder of your CD-ROM drive. Open this file in your browser and you'll see the values contained in the four properties of the Navigator object for your current browser. The results for Navigator 4 and IE 4 are shown in figure 3d. You can view or use the source code for the file with any HTML editor, text editor and most Web browsers.

 


Web resources

Webreference tutorial: You'll find an excellent tutorial on the web which uses a slightly different approach to tailoring script for a particular browser. This program does not require a second page to be loaded and instead uses frames in an interesting way which is worth studying if you want to extend your JavaScript knowledge. You'll find this JavaScript tip of the Week tutorial on Webreference's site at:

http://www.webreference.com/javascript/961028/part01.html

Netscape's authoring guide:
http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/index.html

 

Yahoo's JavaScript Index:
http://www.yahoo.com/Computers_and_Internet/Programming_Languages/JavaScript/

 

Books

These books will be a valuable addition to your library:

For beginners: JavaScript Interactive Course, Waite Group Press

For more accomplished users: Special Edition, Using JavaScript, by M Reynolds and A Wooldridge.

 


Top of page
|What's New | Net Guides | Web Workshop | Net Sites | About PC User |
| Games | Education | General & Business | Online Tools | Utilities |
| Patches & Support files | PC User Interactive |

All text © 1997 Australian Consolidated Press - PC User Magazine