简体   繁体   中英

What is best way to determine if the user is actually using Safari?

确定用户是否实际使用Safari的最佳方法是什么?

You might want to read http://www.quirksmode.org/js/detect.html and then http://www.quirksmode.org/js/support.html and understand that the best you can do is probe what the user tells you is the browser they are using (this would be the User_Agent). You can never say with 100% certainty that they are using a given browser, because you have to rely on what is transmitted from the browser and it can be overriden at many points.

However, the quirksmode.org solution is usually the best way to deal with a single check across all engines and browsers.

Are you using a particular javascript framework that could allow you to more accurately determine what they're using? What serverside language are you using? (and as somebody else asked, why Safari only?)

Others have already noted that browser sniffing is not ideal, but there are cases where a bug only happens in a browser and it can be hard to detect. In that case, you can do browser sniffing and hope the user agent string is valid. Here's what Ext-JS does.

var ua = navigator.userAgent.toLowerCase();
var isChrome = /\bchrome\b/.test(ua);
var isSafari = !isChrome && /safari/.test(ua);

Line 62 of view-source:http://dev.sencha.com/deploy/dev/docs/source/Ext.html has all of Ext's browser detection in an easy to read format.

This is brittle and will probably not work as new versions and browsers come out.

Tell us why you're testing for safari, so we can make sure there isn't a better way to achieve what you want.

if(navigator.userAgent.toLowerCase().indexOf("safari") != -1 && navigator.userAgent.toLowerCase().indexOf("chrome") == -1){
    // you code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM