browser区分Chrome和Safari

browser区分Chrome和Safari

本文介绍了使用jQuery.browser区分Chrome和Safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来能够很容易地从1.4开始标识webkit。但是,我怎样才能使用它来区分Chrome浏览器(反之亦然)?

解决方案

由于Sarfraz没有改正他的答案(谢谢Sarfraz指出我正确的方向),我会在这里发布功能代码。

  var userAgent = navigator.userAgent .toLowerCase(); 
$ .browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

//这是Chrome的一个版本吗?
if($。browser.chrome){
userAgent = userAgent.substring(userAgent.indexOf('chrome /')+7);
userAgent = userAgent.substring(0,userAgent.indexOf('。'));
$ .browser.version = userAgent;
//如果它是chrome,那么jQuery认为它是safari,所以我们必须告诉它它不是
$ .browser.safari = false;
}

//这是Safari的一个版本吗?
if($。browser.safari){
userAgent = userAgent.substring(userAgent.indexOf('version /')+8);
userAgent = userAgent.substring(0,userAgent.indexOf('。'));
$ .browser.version = userAgent;
}


It seems jQuery.browser is able to identify webkit rather easily as of 1.4. But how can I use it to distinguish Chrome from Safari (and visa-versa)?

解决方案

Since Sarfraz has not corrected his answer (thank you Sarfraz for pointing me in the correct direction), I will post functioning code here.

var userAgent = navigator.userAgent.toLowerCase();
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

// Is this a version of Chrome?
if($.browser.chrome){
  userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  $.browser.version = userAgent;
  // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
  $.browser.safari = false;
}

// Is this a version of Safari?
if($.browser.safari){
  userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  $.browser.version = userAgent;
}

这篇关于使用jQuery.browser区分Chrome和Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:37