I recommend using the tiny javascript library Bowser, yes no r. It is based on the navigator.userAgent
and quite well tested for all browsers including iphone, android etc.
https://github.com/ded/bowser
You can use simply say:
if (bowser.msie && bowser.version <= 6) {
alert('Hello IE');
} else if (bowser.firefox){
alert('Hello Foxy');
} else if (bowser.chrome){
alert('Hello Chrome');
} else if (bowser.safari){
alert('Hello Safari');
} else if(bowser.iphone || bowser.android){
alert('Hello mobile');
}