Browser detection javascript

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

if(navigator.userAgent.indexOf(‘Safari’)!=-1){
alert(‘safari’);
} else if(navigator.userAgent.indexOf(‘Gecko’)!=-1) {
alert(‘mozilla’);
}  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.

thanks,

Sachin (samsami2um@gmail.com)

Advertisements

Browser detection html code

Need some method for browser detection with HTML itself, here it is.

With this method you can detect whether your browser is IE or non-ie (i.e. mozilla firefox, safari, opera etc.)

Now you won’t have to write server side script such as php, .net and others or client side script such as javascript or vb script for browser detection and accordingly output your HTML code.

The method is simple as shown below:

For displaying HTML code only in IE:

<!--[if IE]> Here comes the HTML Code <![endif]-->

For displaying HTML code in browsers other than IE:

<!--[if !IE]>--> Here comes the HTML Code <!--<![endif]-->

Also you can have your code work with different versions of IE.
i.e.
<!--[if lt IE 7]> Here comes the HTML Code <![endif]-->

here the HTML code will work only for IE having version less than IE 7. and here lt means less than

also you can even write lte which means less than equal to

Hope this method help you out with your problems.

Sachin (samsami2u@gmail.com)