Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


JavaScript Browser Detection


Reply
Views: 1670  
Thread Tools Rate Thread
  #1  
Old 05-03-2009, 06:04 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
Default 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:
  • appName - holds the name of the browser
  • appVersion - holds, among other things, the version of the browser



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










Reply With Quote
  #2  
Old 05-03-2009, 06:05 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
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
Reply With Quote
  #3  
Old 05-03-2009, 06:05 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
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
Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)