Last active
September 8, 2020 10:11
-
-
Save yousifalraheem/f61afaaf30c2db5ab9e0910e72edd886 to your computer and use it in GitHub Desktop.
Detect browser in use with JavaScript
This file contains 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
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