This string is returned to a web server in the USER-AGENT: header of an HTTP request. This is often logged and processed to analyze the distribution of browser types across the user base for a site.
The userAgent string contains much useful information and generally includes everything the appVersion and appName properties would have told you as well.
Here are some typical values:
Mozilla/4.7 (Macintosh; I; PPC)
Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)
Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)
Beware that some customized browsers will encode a variety of strange textual values into their userAgent property.
While this will only affect the user of the browser when they run your script, the nature of the userAgent string may cause your script to fail.
The following unusual values have been encountered in userAgent strings:
- Null characters
- Control characters
- URL strings
- HTML
- C-Code
- JavaScript
- GIF image data
- Very large text blocks
These appear to be intended to cause server difficulties and possibly malfunctions in logging analysis systems. They may, as a side effect cause your script to crash and burn too.
// A function to normalize the user agent string function getBrowserType() { var myUserAgent; var myMajor; myUserAgent = navigator.userAgent.toLowerCase(); myMajor = parseInt(navigator.appVersion); if( (myUserAgent.indexOf('mozilla') != -1) && (myUserAgent.indexOf('spoofer') == -1) && (myUserAgent.indexOf('compatible') == -1) && (myUserAgent.indexOf('opera') == -1) && (myUserAgent.indexOf('webtv') == -1) ) { if (myMajor > 4) { return "nav6"; } if (myMajor > 3) { return "nav4"; } return "nav"; } if (myUserAgent.indexOf("msie") != -1) { if (myMajor > 4) { return "ie5"; } if (myMajor > 3) { return "ie4"; } return "ie"; } return "other"; }
See also: | server.agent |
Prev | Home | Next |
Navigator.taintEnabled() | Up | Navigator.userLanguage |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |