Browser detection javascript

heres a javascript for the detection of the web browser used by the end user:

} else if(navigator.userAgent.indexOf(‘Gecko’)!=-1) {
}  else {
if(navigator.userAgent.indexOf(‘MSIE 7.0’)!=-1) {
alert(‘ie 7’);
} else {
alert(‘ie 6’);

now i’ll explain you the above script:

navigator.userAgent returns the full description of the user agent or web browser used by the client or end user.

indexOf() function returns the character position of the string passed to it in a given string.

i.e.  navigator.userAgent.indexOf(‘Safari’)!=-1

here, navigator.userAgent is the “search” string  and Safari is the “search keyword”; so if Safari is found in the “search” string, the comparison will return a non-negative number and if not found will return -1

so if the search keyword is found in the search string the condition becomes true.


Sachin (

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s