JavaScript : Detecting the Visitor's Browser
Contents ]
Sékine Coulibaly

Browser, Who Is Your Master?

Finally, the last function identifies the platform used, in this case: Windows, Macintosh or various shades of Unix.

We will now retrieve the system name from the value returned by the appVersion property, with the help of the indexOf method. This method returns the position of the first occurrence of a given string in another string, or -1 if there is no match:

function Plateforme(){
  if (navigator.appVersion.indexOf("Win") > -1) {return "Windows";}
  if (navigator.appVersion.indexOf("Macintosh") > -1) {return "Macintosh";}
  if (navigator.appVersion.indexOf("X11") > -1) {return "Unix";}
  if (navigator.appVersion.indexOf("Unix") > -1) {return "Unix";}
  if (navigator.appVersion.indexOf("Linux") > -1) {return "Unix";}
  return "Unknown";}

The indexOf method searches the value of the appVersion property successively for the strings "Win", "Macintosh", "X11", "Unix" and "Linux". A match is found when the function returns a value greater than -1.

The right method. We have also used the indexOf method to retrieve the browser's name. Indeed, some versions can associate pieces of information other than the browser name with the appName property.