从1.4版本开始,jQuery.browser似乎能够轻松识别webkit。但是,如何使用它来区分Chrome和Safari(反之亦然)?

最佳答案

由于Sarfraz尚未纠正他的答案(感谢Sarfraz将我指向正确的方向),因此我将在此处发布功能代码。

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 - 使用jQuery.browser从Safari中区分Chrome,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3303858/

10-11 11:43