#1
|
|||
|
|||
JavaScript Browser Detection
The JavaScript Navigator object contains information about the visitor's browser.
Browser Detection Almost everything in this tutorial works on all JavaScript-enabled browsers. However, there are some things that just don't work on certain browsers - especially on older browsers. So, sometimes it can be very useful to detect the visitor's browser, and then serve up the appropriate information. The best way to do this is to make your web pages smart enough to look one way to some browsers and another way to other browsers. JavaScript includes an object called the Navigator object, that can be used for this purpose. The Navigator object contains information about the visitor's browser name, version, and more. The Navigator Object The JavaScript Navigator object contains all information about the visitor's browser. We are going to look at two properties of the Navigator object:
Example The variable browser in the example above holds the name of the browser, i.e. "Netscape" or "Microsoft Internet Explorer". The appVersion property in the example above returns a string that contains much more information than just the version number, but for now we are only interested in the version number. To pull the version number out of the string we are using a function called parseFloat(), which pulls the first thing that looks like a decimal number out of a string and returns it. IMPORTANT! The version number is WRONG in IE 5.0 or later! Microsoft starts the appVersion string with the number 4.0. in IE 5.0 and IE 6.0!!! Why did they do that??? However, JavaScript is the same in IE6, IE5 and IE4, so for most scripts it is ok. The example below displays a different alert, depending on the visitor's browser: Example |
#2
|
|||
|
|||
More Examples
More details about the visitor's browser ----- Browser: Netscape Browserversion: 5.0 (Windows; en-GB) Code: Mozilla Platform: Win32 Cookies enabled: true Browser's user agent header: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9 |
#3
|
|||
|
|||
All details about the visitor's browser
------- CodeName=Mozilla MinorVersion=undefined Name=Netscape Version=5.0 (Windows; en-GB) CookieEnabled=true CPUClass=undefined OnLine=true Platform=Win32 UA=Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.9) Gecko/2009040821 Firefox/3.0.9 BrowserLanguage=undefined SystemLanguage=undefined UserLanguage=undefined |