Last active
October 20, 2021 10:59
-
-
Save kanthvallampati/b82c6674f0b35eef085c8fc5ea0a9b7d to your computer and use it in GitHub Desktop.
JavaScript snippet to detect the user browser with version
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 detectBrowser() { | |
let ua = window.navigator.userAgent; | |
if (ua.indexOf('Edg') > -1) { | |
let M = ua.match(/(Edg)\/?\s*(\.?\d+(\.\d+)*)/i); | |
return 'Edge '+ M[2]; | |
} else if (ua.indexOf('OPR') > -1) { | |
let M = ua.match(/(OPR)\/?\s*(\.?\d+(\.\d+)*)/i); | |
return 'Opera '+ M[2]; | |
} else { | |
let tem; | |
let M = ua.match(/(chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | |
if (M && (tem = ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; | |
M = M? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?']; | |
return M[0]+' '+M[1]; | |
} | |
} | |
detectBrowser(); // browser version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment