In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else?

后端 未结 13 1910
萌比男神i
萌比男神i 2020-12-01 07:13
if(firefox and is on a computer){
alert(\'using firefox on a computer\')
}else{
alert(\"using something else!\");
}

How can I do this?

13条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 08:03

    navigator.sayswho= (function(){
      var N= navigator.appName, ua= navigator.userAgent, tem;
      var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
      if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
      M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
      return M.join(' ');
     })();
    

    as the name suggests, this is who the browser says it is- but use object detection before asking it to actually do anything...

    I use it for logging errors from users and in testing code in multiple browsers- where I know the userAgent strings.

提交回复
热议问题