Last active
October 28, 2021 13:37
-
-
Save ninty9notout/6926f56e530fe675450b524329402f00 to your computer and use it in GitHub Desktop.
Detect browser and add class to <html> tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit to https://stackoverflow.com/a/9851769 | |
(function () { | |
// 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 = (!!navigator.userAgent.match(/iPad/i) || !!navigator.userAgent.match(/iPhone/i) && !!navigator.userAgent.match(/WebKit/i) && !navigator.userAgent.match(/CriOS/i)) || /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification)); | |
// Internet Explorer 6-11 | |
var isIE = /*@cc_on!@*/false || !!document.documentMode; | |
// Edge 20+ | |
var isEdge = !isIE && !!window.StyleMedia; | |
// Chrome 1+ | |
var isChrome = !!window.chrome && !!window.chrome.webstore; | |
// Blink engine detection | |
var isBlink = (isChrome || isOpera) && !!window.CSS; | |
// Combine them all | |
var classes = []; | |
if (isOpera) { | |
classes.push('is-opera'); | |
} | |
if (isFirefox) { | |
classes.push('is-firefox'); | |
} | |
if (isSafari) { | |
classes.push('is-safari'); | |
} | |
if (isIE) { | |
classes.push('is-ie'); | |
} | |
if (isEdge) { | |
classes.push('is-edge'); | |
} | |
if (isChrome) { | |
classes.push('is-chrome'); | |
} | |
if (isBlink) { | |
classes.push('is-blink'); | |
} | |
document.documentElement.className += ' ' + classes.join(' '); | |
})(); | |
// Minified | |
!function(){var i=!!window.opr&&!!opr.addons||!!window.opera||navigator.userAgent.indexOf(" OPR/")>=0,e="undefined"!=typeof InstallTrigger,o=!!navigator.userAgent.match(/iPad/i)||!!navigator.userAgent.match(/iPhone/i)&&!!navigator.userAgent.match(/WebKit/i)&&!navigator.userAgent.match(/CriOS/i)||/constructor/i.test(window.HTMLElement)||"[object SafariRemoteNotification]"===(!window.safari||"undefined"!=typeof safari&&safari.pushNotification).toString(),n=!!document.documentMode,t=!n&&!!window.StyleMedia,a=!!window.chrome&&!!window.chrome.webstore,r=(a||i)&&!!window.CSS,s=[];i&&s.push("is-opera"),e&&s.push("is-firefox"),o&&s.push("is-safari"),n&&s.push("is-ie"),t&&s.push("is-edge"),a&&s.push("is-chrome"),r&&s.push("is-blink"),document.documentElement.className+=" "+s.join(" ")}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment