Skip to content

Instantly share code, notes, and snippets.

@ziqq
Last active January 24, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziqq/51536afab76f732514536c1059dbcd9a to your computer and use it in GitHub Desktop.
Save ziqq/51536afab76f732514536c1059dbcd9a to your computer and use it in GitHub Desktop.
Test browser type
function _detectBrowserType() {
const html = document.querySelector('html');
const isChrome = !!window.chrome && !isOpera;
const isFirefox = typeof window.InstallTrigger !== 'undefined';
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
const isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
const isExplorer = typeof document !== 'undefined' && !!document.documentMode && !isEdge;
// if (/windows phone/i.test(userAgent)) {
// html.classList.add('is-windows');
// }
if (/android/i.test(userAgent)) {
html.classList.add('is-android');
} else {
html.classList.remove('is-android');
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
html.classList.add('is-ios');
} else {
html.classList.remove('is-ios');
}
if (isChrome) {
html.classList.add('is-chrome');
html.classList.remove('is-safari');
html.classList.remove('is-firefox');
} else if (isSafari) {
html.classList.add('is-safari');
html.classList.remove('is-chrome');
html.classList.remove('is-firefox');
} else if (isFirefox) {
html.classList.add('is-firefox');
html.classList.remove('is-chrome');
html.classList.remove('is-safari');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment