Skip to content

Instantly share code, notes, and snippets.

@lyquix-owner
Last active September 10, 2023 04:24
Show Gist options
  • Save lyquix-owner/34a3ed98b990c3c61151d30de2f42702 to your computer and use it in GitHub Desktop.
Save lyquix-owner/34a3ed98b990c3c61151d30de2f42702 to your computer and use it in GitHub Desktop.
getBrowser: function that returns the name, version and type of browser based on browser user agent
// returns the browser name, type and version, and sets body classes
// detects major browsers: IE, Edge, Firefox, Chrome, Safari, Opera, Android
// based on: https://github.com/ded/bowser
// list of user agen strings: http://www.webapps-online.com/online-tools/user-agent-strings/dv
function getBrowser(){
var ua = navigator.userAgent, browser;
// helper functions to deal with common regex
function getFirstMatch(regex) {
var match = ua.match(regex);
return (match && match.length > 1 && match[1]) || '';
}
function getSecondMatch(regex) {
var match = ua.match(regex);
return (match && match.length > 1 && match[2]) || '';
}
// start detecting
if (/opera|opr/i.test(ua)) {
browser = {
name: 'Opera',
type: 'opera',
version: getFirstMatch(/version\/(\d+(\.\d+)?)/i) || getFirstMatch(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i)
}
} else if (/msie|trident/i.test(ua)) {
browser = {
name: 'Internet Explorer',
type: 'msie',
version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i)
}
} else if (/chrome.+? edge/i.test(ua)) {
browser = {
name: 'Microsft Edge',
type: 'msedge',
version: getFirstMatch(/edge\/(\d+(\.\d+)?)/i)
}
} else if (/chrome|crios|crmo/i.test(ua)) {
browser = {
name: 'Google Chrome',
type: 'chrome',
version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
}
} else if (/firefox/i.test(ua)) {
browser = {
name: 'Firefox',
type: 'firefox',
version: getFirstMatch(/(?:firefox)[ \/](\d+(\.\d+)?)/i)
}
} else if (!(/like android/i.test(ua)) && /android/i.test(ua)) {
browser = {
name: 'Android',
type: 'android',
version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
}
} else if (/safari/i.test(ua)) {
browser = {
name: 'Safari',
type: 'safari',
version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
}
} else {
browser = {
name: getFirstMatch(/^(.*)\/(.*) /),
version: getSecondMatch(/^(.*)\/(.*) /)
}
browser.type = browser.name.toLowerCase().replace(/\s/g, '');
}
return browser;
}
@drishag
Copy link

drishag commented Sep 10, 2023

// returns the browser name, type and version, and sets body classes
// detects major browsers: IE, Edge, Firefox, Chrome, Safari, Opera, Android
// based on: https://github.com/ded/bowser
// list of user agen strings: http://www.webapps-online.com/online-tools/user-agent-strings/dv
function getBrowser(){
var ua = navigator.userAgent, browser;

// helper functions to deal with common regex
function getFirstMatch(regex) {
	var match = ua.match(regex);
	return (match && match.length > 1 && match[1]) || '';
}

function getSecondMatch(regex) {
	var match = ua.match(regex);
	return (match && match.length > 1 && match[2]) || '';
}

// start detecting
if (/opera|opr/i.test(ua)) {
	browser = {
		name: 'Opera',
		type: 'opera',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i) || getFirstMatch(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i)
	}
}  else if (/msie|trident/i.test(ua)) {
	browser = {
		name: 'Internet Explorer',
		type: 'msie',
		version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i)
	}
} else if (/chrome.+? edge/i.test(ua)) {
	browser = {
		name: 'Microsft Edge',
		type: 'msedge',
		version: getFirstMatch(/edge\/(\d+(\.\d+)?)/i)
	}
} else if (/chrome|crios|crmo/i.test(ua)) {
	browser = {
		name: 'Google Chrome',
		type: 'chrome',
		version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
	}
} else if (/firefox/i.test(ua)) {
	browser = {
		name: 'Firefox',
		type: 'firefox',
		version: getFirstMatch(/(?:firefox)[ \/](\d+(\.\d+)?)/i)
	}
} else if (!(/like android/i.test(ua)) && /android/i.test(ua)) {
	browser = {
		name: 'Android',
		type: 'android',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
	}
} else if (/safari/i.test(ua)) {
	browser = {
		name: 'Safari',
		type: 'safari',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
	}
} else {
	browser = {
		name: getFirstMatch(/^(.*)\/(.*) /),
		version: getSecondMatch(/^(.*)\/(.*) /)
	}
	browser.type = browser.name.toLowerCase().replace(/\s/g, '');
}
return browser;

}

@drishag
Copy link

drishag commented Sep 10, 2023

// returns the browser name, type and version, and sets body classes
// detects major browsers: IE, Edge, Firefox, Chrome, Safari, Opera, Android
// based on: https://github.com/ded/bowser
// list of user agen strings: http://www.webapps-online.com/online-tools/user-agent-strings/dv
function getBrowser(){
var ua = navigator.userAgent, browser;

// helper functions to deal with common regex
function getFirstMatch(regex) {
	var match = ua.match(regex);
	return (match && match.length > 1 && match[1]) || '';
}

function getSecondMatch(regex) {
	var match = ua.match(regex);
	return (match && match.length > 1 && match[2]) || '';
}

// start detecting
if (/opera|opr/i.test(ua)) {
	browser = {
		name: 'Opera',
		type: 'opera',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i) || getFirstMatch(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i)
	}
}  else if (/msie|trident/i.test(ua)) {
	browser = {
		name: 'Internet Explorer',
		type: 'msie',
		version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i)
	}
} else if (/chrome.+? edge/i.test(ua)) {
	browser = {
		name: 'Microsft Edge',
		type: 'msedge',
		version: getFirstMatch(/edge\/(\d+(\.\d+)?)/i)
	}
} else if (/chrome|crios|crmo/i.test(ua)) {
	browser = {
		name: 'Google Chrome',
		type: 'chrome',
		version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
	}
} else if (/firefox/i.test(ua)) {
	browser = {
		name: 'Firefox',
		type: 'firefox',
		version: getFirstMatch(/(?:firefox)[ \/](\d+(\.\d+)?)/i)
	}
} else if (!(/like android/i.test(ua)) && /android/i.test(ua)) {
	browser = {
		name: 'Android',
		type: 'android',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
	}
} else if (/safari/i.test(ua)) {
	browser = {
		name: 'Safari',
		type: 'safari',
		version: getFirstMatch(/version\/(\d+(\.\d+)?)/i)
	}
} else {
	browser = {
		name: getFirstMatch(/^(.*)\/(.*) /),
		version: getSecondMatch(/^(.*)\/(.*) /)
	}
	browser.type = browser.name.toLowerCase().replace(/\s/g, '');
}
return browser;

}

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