Browser Detection Methods

A browser is identified by its name (Netscape, Firefox, Internet Explorer, and so on) combined with its version number. Your JavaScript needs to determine both of these items. There are two ways to approach this task: a quick but rough method and a slightly less quick but more accurate method.

Quick-but-Rough Browser Detection

In general, the line

var browser_name = navigator.appName;

determines who made the browser. If the user is using a Netscape browser, the variable browser_name will be set to the string "Netscape". If it's a Microsoft Internet Explorer browser, browser_name will be set to "Microsoft Internet Explorer". Every JavaScript-enabled browser must have the variable navigator.appName. If you use Opera, navigator.appName ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.