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 (