Skip to content

Instantly share code, notes, and snippets.

@yousifalraheem
Last active September 8, 2020 10:11
Show Gist options
  • Save yousifalraheem/f61afaaf30c2db5ab9e0910e72edd886 to your computer and use it in GitHub Desktop.
Save yousifalraheem/f61afaaf30c2db5ab9e0910e72edd886 to your computer and use it in GitHub Desktop.
Detect browser in use with JavaScript
function whichBrowser() {
if (isFirefox()) {
return "Firefox";
} else if (isEdge()) {
return "Edge";
} else if (isIE()) {
return "Internet Explorer";
} else if (isOpera()) {
return "Opera";
} else if (isVivaldi()) {
return "Vivalid";
} else if (isChrome()) {
return "Chrome";
} else if (isSafari()) {
return "Safari";
} else {
return "Unknown";
}
}
function agentHas(keyword) {
return navigator.userAgent.toLowerCase().search(keyword.toLowerCase()) > -1;
}
function isIE() {
return !!document.documentMode;
}
function isSafari() {
return (!!window.ApplePaySetupFeature || !!window.safari) && agentHas("Safari") && !agentHas("Chrome") && !agentHas("CriOS");
}
function isChrome() {
return agentHas("CriOS") || agentHas("Chrome") || !!window.chrome;
}
function isFirefox() {
return agentHas("Firefox") || agentHas("FxiOS") || agentHas("Focus");
}
function isEdge() {
return agentHas("Edg");
}
function isOpera() {
return agentHas("OPR");
}
function isVivaldi() {
return agentHas("Vivaldi");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment