Spread the word

Sunday, May 11, 2008

How to detect user's Browser in JavaScript - Which Browser your visitor is using?

Many times you will need to show up different messages or include different elements according to the browser your user is using. For this you need to detect which Browser you visitor is using to browse your Website. There are many techniques to detect user's browsers, here we will detect browsers through JavaScript. Enclose these codes in the Script tag for JavaScript.


//Detect Firefox
if(navigator.userAgent.indexOf(”Firefox”)!=-1){
var versionindex=navigator.userAgent.indexOf(”Firefox”)+8
if (parseInt(navigator.userAgent.charAt(versionindex))>=1)
alert(”You are using Firefox 1.x or above”)
}

//Detect Netscape 4.7+
if (navigator.appName==”Netscape”&&parseFloat(navigator.appVersion)>=4.7)
alert(”You are using Netscape 4.7+”)


//Detect Opera
if(navigator.userAgent.indexOf(”Opera”)!=-1){
var versionindex=navigator.userAgent.indexOf(”Opera”)+6
if (parseInt(navigator.userAgent.charAt(versionindex))>=8)
alert(”You are using Opera 8 or 9″)
}

//Detect IE5.5+
version=0
if (navigator.appVersion.indexOf(”MSIE”)!=-1){
temp=navigator.appVersion.split(”MSIE”)
version=parseFloat(temp[1])
}

if (version>=5.5) //NON IE browser will return 0
alert(”You’re using IE5.5+”)


So from now start making your websites look 100% accurate in every browsers since you can select different CSS files for different browsers.
If you want to know about browser specific CSS Hacks please see this Article on Browser Specific CSS Hacks. Here you can learn CSS techniques to display your website perfectly on different browsers and operating systems through some nice CSS hacks.

0 comments:

Post a Comment