使用jQuery检测Safari [英] Detect Safari using jQuery

查看:277
本文介绍了使用jQuery检测Safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然两者都是基于Webkit的浏览器,但是在URL中的Safari urlencodes引号却没有Chrome。

Though both are Webkit based browsers, Safari urlencodes quotation marks in the URL while Chrome does not.

因此我需要在JS中区分这两者。

Therefore I need to distinguish between these two in JS.

jQuery的浏览器检测文档标记safari 已弃用。

jQuery's browser detection docs mark "safari" as deprecated.

是否有更好的方法,或者我现在只坚持弃用值?

Is there a better method or do I just stick with the deprecated value for now?

推荐答案

混合使用功能检测 Useragent string:

Using a mix of feature detection and Useragent string:

    var is_opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    var is_chrome = !!window.chrome && !is_opera;
    var is_explorer= typeof document !== 'undefined' && !!document.documentMode && !isEdge;
    var is_firefox = typeof window.InstallTrigger !== 'undefined';
    var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

用法:

if(is_safari)警报('Safari');

或仅限Safari,请使用:

Or for Safari only, use this :

if ( /^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {alert('Its Safari');}

这篇关于使用jQuery检测Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆