Home > Web Front-end > JS Tutorial > How Can I Reliably Detect Safari, Chrome, Firefox, IE, and Opera Browsers Using Duck Typing?

How Can I Reliably Detect Safari, Chrome, Firefox, IE, and Opera Browsers Using Duck Typing?

DDD
Release: 2024-12-18 13:41:16
Original
583 people have browsed it

How Can I Reliably Detect Safari, Chrome, Firefox, IE, and Opera Browsers Using Duck Typing?

Detect Safari, Chrome, IE, Firefox, and Opera Browsers with Duck-Typing

Determining the user's browser is often necessary for redirecting them to the appropriate download link for browser-specific extensions. However, relying on the User agent string for browser detection is unreliable due to its susceptibility to spoofing.

A more reliable method, known as duck-typing, can be used to identify browsers based on their specific characteristics. Here's a breakdown:

Opera 8.0 :

var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
Copy after login

Firefox 1.0 :

var isFirefox = typeof InstallTrigger !== 'undefined';
Copy after login

Safari 3.0 :

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

Internet Explorer 6-11:

var isIE = /*@cc_on!@*/false || !!document.documentMode;
Copy after login

Edge 20 :

var isEdge = !isIE && !!window.StyleMedia;
Copy after login

Chrome 1 - 79:

var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
Copy after login

Edge (based on chromium) detection:

var isEdgeChromium = isChrome && (navigator.userAgent.indexOf("Edg") != -1);
Copy after login

Blink engine detection:

var isBlink = (isChrome || isOpera) && !!window.CSS;
Copy after login

Once you have detected the browser using these methods, you can redirect users to the appropriate download link for their browser-specific extension.

However, it's crucial to emphasize that you should only use browser detection when necessary, such as displaying browser-specific installation instructions. As a general best practice, focus on feature detection whenever possible.

The above is the detailed content of How Can I Reliably Detect Safari, Chrome, Firefox, IE, and Opera Browsers Using Duck Typing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template