EN
JavaScript - detect Apple Safari web browser
3
points
In this short article, we would like to show how using JavaScript, detect Apple Safari web browser on Apple devices.
Quick solution:
// ONLINE-RUNNER:browser;
const appleExpression = /Apple/i;
const safariExpression = /Safari/i;
const isAppleSafari = () => {
return appleExpression.test(navigator.vendor) &&
safariExpression.test(navigator.userAgent);
};
// Usage example:
if (isAppleSafari()) {
console.log('You are on Apple Safari web browser.');
} else {
console.log('You are not on Apple Safari web browser.');
}
Warning:
User-Agent
can be changed by advanced user that makes lead to some errors - if it may be your case, check alternative solution too.
Alternative solution
In this solution, we test only user agent to detect web browser.
// ONLINE-RUNNER:browser;
const platformExpression = /Mac|iPhone|iPod|iPad/i;
const rejectedExpression = /Chrome|Android|CriOS|FxiOS|EdgiOS/i;
const expectedExpression = /Safari/i;
const isAppleSafari = () => {
const agent = navigator.userAgent;
if (rejectedExpression.test(agent)) {
return false;
}
return platformExpression.test(agent) && expectedExpression.test(agent);
};
// Usage example:
if (isAppleSafari()) {
console.log('You are on Apple Safari web browser.');
} else {
console.log('You are not on Apple Safari web browser.');
}
Warning:
Apple Safar web browser was released last time on Windows in 2012, that makes why we do not consider other platforms in the above logic.
Example test
// ONLINE-RUNNER:browser;
const platformExpression = /Mac|iPhone|iPod|iPad/i;
const rejectedExpression = /Chrome|Android|CriOS|FxiOS|EdgiOS/i;
const expectedExpression = /Safari/i;
function isAppleSafari(agent) {
if (rejectedExpression.test(agent)) {
return false;
}
return platformExpression.test(agent) && expectedExpression.test(agent);
}
// Testing data:
const agents = [
/*
Source: https://dirask.com/snippets/text/Example+User-Agent+values
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Browser + Platform User-Agent
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
'Android 4.0.2 Browser ', 'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
'Android 2.3 Browser ', 'Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
'', '',
'BlackBerry -- BB10 ', 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.1+ (KHTML, like Gecko) Version/10.0.0.1337 Mobile Safari/537.1+',
'BlackBerry -- PlayBook 2.1 ', 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+',
'BlackBerry -- 9900 ', 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+',
'', '',
'Chrome -- Android Mobile ', 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Mobile Safari/537.36',
'Chrome -- Android Mobile (high-end) ', 'Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Mobile Safari/537.36',
'Chrome -- Android Tablet ', 'Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36',
'Chrome -- iPhone ', 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/%s Mobile/15E148 Safari/604.1',
'Chrome -- iPad ', 'Mozilla/5.0 (iPad; CPU OS 13_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/%s Mobile/15E148 Safari/604.1',
'Chrome -- Chrome OS ', 'Mozilla/5.0 (X11; CrOS x86_64 10066.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36',
'Chomre -- Mac ', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36',
'Chrome -- Windows ', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36',
'', '',
'Firefox -- Android Mobile ', 'Mozilla/5.0 (Android 4.4; Mobile; rv:70.0) Gecko/70.0 Firefox/70.0',
'Firefox -- Android Tablet ', 'Mozilla/5.0 (Android 4.4; Tablet; rv:70.0) Gecko/70.0 Firefox/70.0',
'Firefox -- iPhone ', 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4',
'Firefox -- iPad ', 'Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4',
'Firefox -- Mac ', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0',
'Firefox -- Windows ', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:70.0) Gecko/20100101 Firefox/70.0',
'', '',
'Googlebot ', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Googlebot Desktop ', 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/%s Safari/537.36',
'Googlebot Smartphone ', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'', '',
'Internet Explorer 11 ', 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Internet Explorer 10 ', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)',
'Internet Explorer 9 ', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
'Internet Explorer 8 ', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
'Internet Explorer 7 ', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'', '',
'Microsoft Edge (Chroimum) -- Windows ', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36 Edg/%s',
'Microsoft Edge (Chromium) -- Mac ', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Chrome/%s Safari/604.1 Edg/%s',
'Microsoft Edge -- iPhone ', 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 EdgiOS/44.5.0.10 Mobile/15E148 Safari/604.1',
'Microsoft Edge -- iPad ', 'Mozilla/5.0 (iPad; CPU OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 EdgiOS/44.5.2 Mobile/15E148 Safari/605.1.15',
'Microsoft Edge -- Android Mobile ', 'Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057',
'Microsoft Edge -- Android Tablet ', 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Safari/537.36 EdgA/42.0.0.2057',
'Microsoft Edge (EdgeHTML) -- Windows ', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19042',
'Microsoft Edge (EdgeHTML) -- XBox ', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19041',
'', '',
'Opera -- Mac ', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36 OPR/65.0.3467.48',
'Opera -- Windows ', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36 OPR/65.0.3467.48',
'Opera (Presto) -- Mac ', 'Opera/9.80 (Macintosh; Intel Mac OS X 10.9.1) Presto/2.12.388 Version/12.16',
'Opera (Presto) -- Windows ', 'Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.16',
'Opera Mobile -- Android Mobile ', 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02',
'Opera Mini -- iOS ', 'Opera/9.80 (iPhone; Opera Mini/8.0.0/34.2336; U; en) Presto/2.8.119 Version/11.10',
'', '',
'Safari -- iPad ', 'Mozilla/5.0 (iPad; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'Safari -- iPhone ', 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'Safari -- Mac ', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15',
'', '',
'UC Browser -- Android Mobile ', 'Mozilla/5.0 (Linux; U; Android 8.1.0; en-US; Nexus 6P Build/OPM7.181205.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.11.1.1197 Mobile Safari/537.36',
'UC Browser -- iOS ', 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)',
'UC Browser -- Windows Phone ', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920) UCBrowser/10.1.0.563 Mobile',
];
// Testing logic:
for (let i = 0; i < agents.length; i += 2) {
const agent = agents[i + 1];
if (agent) {
const result = isAppleSafari(agent);
console.log(result + '\t\t' + agents[i + 0]);
} else {
console.log();
}
}