The User's Environment

In this lesson of the JavaScript tutorial, you will learn...
  1. To detect the user's operating system.
  2. To detect browser information.
  3. To determine if the user's browser supports a specific feature.
  4. To determine if Cookies are turned off.

Why Know the User's Environment?

Although browsers have come a long way in the past several years, they unfortunately do not all support the W3C specifications to the same degree. When writing sophisticated code that is intended to work across multiple browsers and platforms, it is often necessary to branch the code for the different environments.

A very good way to manage this is to create a JavaScript file that stores many environment-related variables that hold flags indicating the type of browser or support for a specific object, property or method.

The code below demonstrates how this file might be structured.

Code Sample: UserEnvironment/Demos/EnvVars.js

//Operating System
var IS_WIN = (navigator.userAgent.indexOf("Win") >= 0);
var IS_MAC = (navigator.userAgent.indexOf("Mac") >= 0);
var IS_UNIX = (navigator.userAgent.indexOf("X11") >= 0);

//Browser Info
var IS_IE = (navigator.appName == "Microsoft Internet Explorer");
var IS_FF = (navigator.userAgent.indexOf("Firefox") >= 0);
var IS_NS = (navigator.vendor != "undefined" && 
     navigator.vendor == "Netscape");
var IS_MOZ = (navigator.userAgent.indexOf("Mozilla/5") >= 0);
var IS_NEW = ((IS_MOZ && parseInt(navigator.appVersion) >= 5) ||
       (IS_IE && parseInt(navigator.appVersion) >= 4));

//Object Support
var SUPPORTS_IMAGES = (typeof document.images != "undefined");

//Cookies
var COOKIES_ON = navigator.cookieEnabled;

Examine UserEnvironment/Demos/UserEnvironment.html to see the properties of your environment.

The User's Environment Conclusion

In this brief lesson, you have learned to determine the settings and capabilities of a user's environment and to respond accordingly.

To continue to learn JavaScript go to the top of this page and click on the next lesson in this JavaScript Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.