Thursday, November 30, 2006

[Script] How to detect client's browser type and version (only support IE, Firefox & Netscape)?

I hypothesize you want to recognize client's type and version. To approach this goal, you should take advantage of navigator object.

The navigator object has the following properties:

  1. appCodeName
    The code name of the browser.
  2. appName
    The name of the browser (ie: Microsoft Internet Explorer).
  3. appVersion
    Version information for the browser (ie: 4.75 [en] (Win98; U)).
  4. cookieEnabled
    Boolean that indicates whether the browser has cookies enabled. IE4 and NS6+.
  5. language
    Returns the default language of the browser version (ie: en-US). NS4 and NS6+ only.
  6. mimeTypes[]
    An array of all MIME types supported by the client. NS4 and NS6+ only. Array is always empty in IE.
  7. platform[]
    The platform of the client's computer (ie: Win32).
  8. plugins
    An array of all plug-ins currently installed on the client. NS4 and NS6+ only. Array is always empty in IE.
  9. systemLanguage
    IE4+ property that returns the default language of the operating system. Similar to NS's language property.
  10. userAgent
    String passed by browser as user-agent header. (ie: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1))
  11. userLanguage
    IE4+ property that returns the preferred language setting of the user. Similar to NS's language property.
Practice 1 - Detect client's browser type:

var typeBrowser;
if(navigator.userAgent.indexOf("MSIE")>-1)
typeBrowser = "MS Internet Explorer";
else if(navigator.userAgent.indexOf("Firefox")>-1)
typeBrowser = "FireFox";
else if(navigator.userAgent.indexOf("Netscape")>-1)
typeBrower = "Netscape";
alert("Your browser is " +
typeBrowser);

Practice 2 - Detect client's brower version:

var BVersion,temp;
if(navigator.userAgent.indexOf("MSIE")>-1){
temp =
navigator.appVersion.split("MSIE");
//because the value of navigator.userAgent in IE is like: 4.0 (compatible;
MSIE 5.5; Windows 98; Hotbar 3.0)
BVersion = parseFloat(temp[1]);
}
else if(navigator.userAgent.indexOf("Firefox")>-1){
temp =
navigator.appVersion.split("Firefox/");
//because the value of navigator.userAgent in IE is like: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
BVersion =
parseFloat(temp[1]);
}
else if
(navigator.userAgent.indexOf("Netscape")>-1){
temp = navigator.appVersion.split("Netscape/");
//because the value of navigator.userAgent in IE is like: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

BVersion = parseFloat(temp[1]);
}
alert("The version of your browser is " + BVersion + " or above");

0 Comments:

Post a Comment

<< Home