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;
Firefox 1.0 :
var isFirefox = typeof InstallTrigger !== 'undefined';
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));
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;
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!