Sometimes it can be useful to detect the visitor's browser, and then serve the appropriate information.
The Navigator object contains information about the visitor's browser name, version, and more.
Sample Code :
<html>
<head>
<script language="javascript" type="text/javascript">
function browserDetect() {
var browserType;
if(/chrome/.test( navigator.userAgent.toLowerCase() )){
browserType="chrome";
}
else if(/webkit/.test( navigator.userAgent.toLowerCase() )){
browserType="safari";
}
else if(/opera/.test( navigator.userAgent.toLowerCase() )){
browserType="opera";
}
else if(/msie/.test( navigator.userAgent.toLowerCase() )){
//browserType="msie";
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
browserType="msie " + ieversion;
}
}
else if(/mozilla/.test( navigator.userAgent.toLowerCase() )){
browserType="mozilla";
}
else
{
browserType="none of these - chrome,safari,opera,msie,mozilla";
}
alert(browserType);
}
</script>
</head>
<body>
<a href="javascript:browserDetect();">Detect Browser</a>
</body>
</html>