I am looking for a way to detect Safari with javascript. I know its been covered many times already but probably something got changed and it does not work anymore. At least in my case.
Here is what I do:
<script>
if(!isSafari()){
alert('not Safari');
} else {
alert('I am Safari');
}
function isSafari(){
var is_safari = navigator.userAgent.indexOf("Safari") > -1;
if(is_safari){
return true;
}
}
</script>
jsbin: http://jsbin.com/ewerof/1
If you run this code in Safari and Chrome you will get the same alert “I am Safari” So how to actually detect Safari only? My Safari version is 4.0.3 just in case if that matters.
Chrome has both ‘Chrome’ and ‘Safari’ inside userAgent string. Safari has only ‘Safari’.
So this works:
if (is_safari) alert(‘Safari’);
Or for Safari only, use this :
Credit: Kabamaru