Skip to content

Instantly share code, notes, and snippets.

@iamdevlinph
Last active July 31, 2020 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iamdevlinph/409807abc775172f059ca71f18b9ac4e to your computer and use it in GitHub Desktop.
Save iamdevlinph/409807abc775172f059ca71f18b9ac4e to your computer and use it in GitHub Desktop.
Check Browser
// https://stackoverflow.com/a/56361977/4110257
function name() {
var agent = window.navigator.userAgent.toLowerCase();
switch (true) {
case agent.indexOf("edge") > -1: return "edge";
case agent.indexOf("edg") > -1: return "chromium based edge (dev or canary)";
case agent.indexOf("opr") > -1 && !!window.opr: return "opera";
case agent.indexOf("chrome") > -1 && !!window.chrome: return "chrome";
case agent.indexOf("trident") > -1: return "ie";
case agent.indexOf("firefox") > -1: return "firefox";
case agent.indexOf("safari") > -1: return "safari";
default: return "other";
}
}
// function name() {
// var browserName = '';
// var userAgent = navigator.userAgent;
// (typeof InstallTrigger !== 'undefined') && (browserName = 'Firefox');
// ( /* @cc_on!@*/ false || !!document.documentMode) && (browserName = 'IE');
// (!!window.chrome && userAgent.match(/OPR/)) && (browserName = 'Opera');
// (!!window.chrome && userAgent.match(/Edge/)) && (browserName = 'Edge');
// (!!window.chrome && !userAgent.match(/(OPR|Edge)/)) && (browserName = 'Chrome');
// /**
// * Expected returns
// * Firefox, Opera, Edge, Chrome
// */
// return browserName;
// }
@iamdevlinph
Copy link
Author

iamdevlinph commented Jul 28, 2020

Updated code for browser checking using the versions above

https://jsfiddle.net/iamdevlinph/thxkdzbn/6/

Working versions:
Firefox - 78.0.2 (64-bit) (detected as firefox)
Chrome - Version 84.0.4147.105 (Official Build) (64-bit) (detected as chrome)
Opera - 70.0.3728.71 (detected as opera)
New Edge Chromium - Version 84.0.522.44 (Official build) (64-bit) (detected as chromium based edge (dev or canary))
Old Edge - Microsoft Edge 44.19041.1.0 (detected as edge)
IE - 11.388.19041.0 (version downgraded but jsfiddle does not run anymore but detected as ie)
Safari - Version 13.1.1 (14609.2.9.1.3) (detected as safari)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment