开发者

jquery check user browser type

开发者 https://www.devze.com 2023-04-04 00:01 出处:网络
I\'m using $.browser to try and check what type of browser users have accessing my site. The problem I have however is that both Safari开发者_如何学Python and Chrome are webkit browsers and I therefor

I'm using $.browser to try and check what type of browser users have accessing my site. The problem I have however is that both Safari开发者_如何学Python and Chrome are webkit browsers and I therefore do not know how to distinguish between the too.

Is there a way to distinguish between safari and chrome so that I can have my site do something different?


function browserTester(browserString) {
    return navigator.userAgent.toLowerCase().indexOf(browserString) > -1;
}

if(browserTester('chrome')) {
    // do stuff for chrome
} else if(browserTester('safari')) {
    //do stuff for safari
}

http://jsfiddle.net/genesis/gm3Na/


You can use the navigator.userAgent to find this out.

Some sample outputs: Chrome:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1

Firefox:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0.2) Gecko/20100101 Firefox/6.0.2

Safari:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3

See here: http://jsfiddle.net/bQrgB/


Here you have a similar topic: Distinguish Chrome from Safari using jQuery.browser

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('safari/') +7);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  $.browser.version = userAgent;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号