Skip to content

Instantly share code, notes, and snippets.

@kanthvallampati
Last active October 20, 2021 10:59
Show Gist options
  • Save kanthvallampati/b82c6674f0b35eef085c8fc5ea0a9b7d to your computer and use it in GitHub Desktop.
Save kanthvallampati/b82c6674f0b35eef085c8fc5ea0a9b7d to your computer and use it in GitHub Desktop.
JavaScript snippet to detect the user browser with version
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