Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph iamdevlinph commented Mar 6, 2019

Working Versions
Opera - 45.0
Firefox - 54.0
Safari - 10.1.2
IE - 11.413.15063
Edge - 40.15063.0.0
Chrome - 59.0.3

Last Updated 21 June, 2017

Fiddle: https://jsfiddle.net/iamdevlinph/1gLxbb6s/

@iamdevlinph

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph iamdevlinph commented Mar 6, 2019

Code for chrome no longer works as of Version 72.0.3626.121 (Official Build) (64-bit)

!!window.chrome.webstore returns false

@iamdevlinph

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph iamdevlinph commented Mar 6, 2019

Changelog;
Updated to work on Chrome.
Removed safari since I don't have safari and I cannot verify it

Working Versions:
Opera - 58.0.3135.79
Firefox - 65.0.2 (64-bit)
IE - 11.413.15063 (JS Fiddle no longer supports IE just paste in Console)
Edge - 44.17763.1.0
Chrome - 72.0.3626.121 (Official Build) (64-bit)

Last Updated 06-March-2019

Fiddle: https://jsfiddle.net/iamdevlinph/1gLxbb6s/30/

Few Notes:
Edge has value for window.chrome
Chrome and Opera has the same window.navigator.vendor value which is Google Inc.
Only kept !!window.chrome for Chrome and check if UserAgent has no OPR or Edge which only exists in Opera and Edge

@iamdevlinph

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph iamdevlinph commented May 16, 2019

As of 16-May-2019

Working versions:
Firefox - 66.0.5 (64-bit)
IE - 11.437.17763.0
Edge - 44.17763.1.0
Opera - 60.0.3255.95
Chrome - 74.0.3729.157 (Official Build) (64-bit)

Fiddle: https://jsfiddle.net/iamdevlinph/1gLxbb6s/30/

@iamdevlinph

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph iamdevlinph commented Jul 28, 2020

As of 28-July-2020

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 Chrome)
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)

Fiddle: https://jsfiddle.net/iamdevlinph/1gLxbb6s/30/

@iamdevlinph

This comment has been minimized.

Copy link
Owner Author

@iamdevlinph 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