The User's Environment
- To detect the user's operating system.
- To detect browser information.
- To determine if the user's browser supports a specific feature.
- 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.
