Wykrywanie Safari za pomocą jQuery

Chociaż obie są przeglądarkami Webkit, Safari urlencodes cudzysłów w adresie URL, podczas gdy Chrome nie.

Dlatego muszę rozróżnić te dwa w JS.

Pliki do wykrywania przeglądarki JQuery zaznaczają "safari" jako przestarzałe.

Czy jest lepsza metoda, Czy mam trzymać się na razie przestarzałej wartości?

Author: AndreKR, 2011-05-05

12 answers

Używając kombinacji feature detection i Useragent string:

    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);
    var is_opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

użycie:
if (is_safari) alert('Safari');

Lub tylko dla Safari, użyj tego:

if ( /^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {alert('Its Safari');}
 307
Author: Panos Kal.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-16 08:51:41

Poniższe identyfikuje Safari 3.0+ i odróżnia je od Chrome:

isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)
 69
Author: brissmyr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-16 14:43:33

Niestety powyższe przykłady wykryją również domyślną przeglądarkę Androida jako Safari, którą nie jest. Używałem navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Android') == -1

 10
Author: user3107045,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-16 11:05:57

Do sprawdzenia Safari użyłem tego:

$.browser.safari = ($.browser.webkit && !(/chrome/.test(navigator.userAgent.toLowerCase())));
if ($.browser.safari) {
    alert('this is safari');
}

To działa poprawnie.

 7
Author: FlamyTwista,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-31 09:46:15

Najwyraźniej jedynym niezawodnym i akceptowanym rozwiązaniem byłoby wykonanie detekcji funkcji w taki sposób:

browser_treats_urls_like_safari_does = false;
var last_location_hash = location.hash;
location.hash = '"blah"';
if (location.hash == '#%22blah%22')
    browser_treats_urls_like_safari_does = true;
location.hash = last_location_hash;
 3
Author: AndreKR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-16 12:50:31

/ Align= "left" / userAgent zawiera iPhone lub iPad word

if (navigator.userAgent.toLowerCase().match(/(ipad|iphone)/)) {
    //is safari
}
 2
Author: ohrlando,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-18 17:42:55

Jeśli sprawdzasz użycie przeglądarki $.browser. Ale jeśli sprawdzasz obsługę funkcji (zalecane), Użyj $.support.

Nie powinieneś używać $.przeglądarka umożliwiająca włączanie / wyłączanie funkcji na stronie. Powodem jest to, że nie jest niezawodny i ogólnie po prostu nie zaleca się.

Jeśli potrzebujesz wsparcia funkcji to polecammodernizr .

 1
Author: John Strickler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-05 14:59:27

Funkcja Ogólna

 getBrowseActive = function (browserName) {
   if (navigator.userAgent.indexOf(browserName) > -1)
     return true;

   return false;
 };
 0
Author: user1330204,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-15 20:49:07
//Check if Safari
  function isSafari() {
      return /^((?!chrome).)*safari/i.test(navigator.userAgent);
  }
//Check if MAC
     if(navigator.userAgent.indexOf('Mac')>1){
        alert(isSafari());
     }

Http://jsfiddle.net/s1o943gb/10/

 0
Author: Code Spy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-27 17:29:23

Bardzo przydatnym sposobem naprawienia tego jest wykrycie wersji przeglądarki webkit i sprawdzenie, czy jest to przynajmniej ta, której potrzebujemy, w przeciwnym razie zrób coś innego.

Używając jQuery wygląda to tak:

"use strict";

$(document).ready(function() {
    var appVersion                  = navigator.appVersion;
    var webkitVersion_positionStart = appVersion.indexOf("AppleWebKit/") + 12;
    var webkitVersion_positionEnd   = webkitVersion_positionStart + 3;
    var webkitVersion               = appVersion.slice(webkitVersion_positionStart, webkitVersion_positionEnd);
	
    console.log(webkitVersion);

    if (webkitVersion < 537) {
        console.log("webkit outdated.");
    } else {
        console.log("webkit ok.");
    };
});

Zapewnia to bezpieczne i trwałe rozwiązanie problemów z różnymi implementacjami webkit przeglądarki.

Szczęśliwego kodowania!
 0
Author: reicek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-22 13:58:35

To określi, czy przeglądarka jest Safari, czy nie.

if(navigator.userAgent.indexOf('Safari') !=-1 && navigator.userAgent.indexOf('Chrome') == -1)
{
    alert(its safari);
}else {
    alert('its not safari');
}
 0
Author: Dushyant Dagar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-17 11:54:37
    // Safari uses pre-calculated pixels, so use this feature to detect Safari
    var canva = document.createElement('canvas');
    var ctx = canva.getContext("2d");
    var img = ctx.getImageData(0, 0, 1, 1);
    var pix = img.data;     // byte array, rgba
    var isSafari = (pix[3] != 0);   // alpha in Safari is not zero
 0
Author: Vitaly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-25 04:55:00