Detect Safari Browser

Detect Safari browser

You can easily use index of Chrome to filter out Chrome:

var ua = navigator.userAgent.toLowerCase(); 
if (ua.indexOf('safari') != -1) {
if (ua.indexOf('chrome') > -1) {
alert("1") // Chrome
} else {
alert("2") // Safari
}
}

How detect Safari browser ONLY on desktop?

const uA = navigator.userAgent;
const vendor = navigator.vendor;
if (/Safari/i.test(uA) && /Apple Computer/.test(vendor) && !/Mobi|Android/i.test(uA)) {
//Desktop Safari
}

How to detect Safari, Chrome, IE, Firefox and Opera browsers?

Googling for browser reliable detection often results in checking the User agent string. This method is not reliable, because it's trivial to spoof this value.

I've written a method to detect browsers by duck-typing.

Only use the browser detection method if it's truly necessary, such as showing browser-specific instructions to install an extension. Use feature detection when possible.

Demo: https://jsfiddle.net/6spj1059/

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification));

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

// Chrome 1 - 79
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);

// Edge (based on chromium) detection
var isEdgeChromium = isChrome && (navigator.userAgent.indexOf("Edg") != -1);

// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;

var output = 'Detecting browsers by ducktyping:<hr>';
output += 'isFirefox: ' + isFirefox + '<br>';
output += 'isChrome: ' + isChrome + '<br>';
output += 'isSafari: ' + isSafari + '<br>';
output += 'isOpera: ' + isOpera + '<br>';
output += 'isIE: ' + isIE + '<br>';
output += 'isEdge: ' + isEdge + '<br>';
output += 'isEdgeChromium: ' + isEdgeChromium + '<br>';
output += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = output;

Detect safari and stop script

I used to check if the user is using safari with this.

var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

Then you can stop the script doing this:

if(isSafari){ 
alert('Is safari');
//Do something

} else {
alert('Is other browser');
// Do something
}

Here is a fiddle so you can play with it.

Here is a snippet for your specific case. I changed the document.write to windows.location in order to make it work.

var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if(isSafari) { var cbuser = { name: '', email: '', message: '' }; var access_token = '7654345676'; window.location = "https://www.example.com/notSafari";} else { var cbuser = { name: '', email: '', message: '' }; var access_token = 'Z2SxfM5dRzKsm3Auhbi4'; window.location = 'https://www.example.com';}

Detect Safari browser in javascript

In safari, window.HTMLElement returns a function which is named HTMLElementConstructor.

So let do this:

/constructor/i.test(function HTMLElementConstructor() {}) // return true

But with other browsers (FF, Chrome), it returns HTMLElement

/constructor/i.test(function HTMLElement() {}) // return false

But thank you for this observation! I hope we can use it as well as the method mentionned here: Detect Safari browser

Determine if user navigated from mobile Safari

UPDATE: This is a very old answer and I cannot delete it because the answer is accepted. Check unwitting's answer below for a better solution.


You should be able to check for the "iPad" or "iPhone" substring in the user agent string:

var userAgent = window.navigator.userAgent;

if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
// iPad or iPhone
}
else {
// Anything else
}

Detect if the user is navigating through the safari mobile browser on iphone

UPDATED:


Try this, for detecting Safari browser in an iPhone:

var isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);

It identifies Safari 3.0+ and distinguishes it from Chrome.

JsFiddle



Related Topics



Leave a reply



Submit