Skip to content

Instantly share code, notes, and snippets.

@typable
Last active Apr 28, 2021
Embed
What would you like to do?
Browser Detection
var Browser = {
type: null,
version: null,
support: function(obj) {
return obj != undefined;
},
is: function(type, version) {
if(type !== Browser.type) {
return false;
}
if(typeof version !== 'undefined') {
if(version === '') {
return false;
}
var exp;
if(/^[\d.]+$/.test(version)) {
if(/^\d+$/.test(version)) {
exp = /^(\d+)\./.exec(Browser.version);
if(exp) {
if(version !== exp[1]) {
return false;
}
}
}
else if(version !== Browser.version) {
return false;
}
}
else if(/^<\d+$/.test(version)) {
exp = /^<(\d+)$/.exec(version);
version = parseInt(exp[1]);
exp = /^(\d+)\./.exec(Browser.version);
if(exp) {
if(!(version > parseInt(exp[1]))) {
return false;
}
}
}
else if(/^>\d+$/.test(version)) {
exp = /^>(\d+)$/.exec(version);
version = parseInt(exp[1]);
exp = /^(\d+)\./.exec(Browser.version);
if(exp) {
if(version >= parseInt(exp[1])) {
return false;
}
}
}
else if(/^\d+\-\d+$/.test(version)) {
exp = /^(\d+)\-(\d+)$/.exec(version);
var version1 = parseInt(exp[1]);
var version2 = parseInt(exp[2]);
exp = /^(\d+)\./.exec(Browser.version);
if(exp) {
if(!(version1 <= parseInt(exp[1]) && version2 >= parseInt(exp[1]))) {
return false;
}
}
}
else {
return false;
}
}
return true;
},
TYPES: {
CHROME: 'Chrome',
FIREFOX: 'Firefox',
IE: 'Internet Explorer',
EDGE: 'Microsoft Edge',
}
};
(function() {
var types = {};
types.chrome = !!window.chrome;
types.firefox = typeof InstallTrigger !== 'undefined';
types.ie = false || !!document.documentMode;
types.edge = !types.ie && !!window.StyleMedia;
var agent = navigator.userAgent;
if(types.chrome) {
Browser.type = Browser.TYPES.CHROME;
var exp = /Chrome\/([\d.]+)/.exec(agent);
if(exp) {
Browser.version = exp[1];
}
}
if(types.firefox) {
Browser.type = Browser.TYPES.FIREFOX;
var exp = /Firefox\/([\d.]+)/.exec(agent);
if(exp) {
Browser.version = exp[1];
}
}
if(types.ie) {
Browser.type = Browser.TYPES.IE;
var exp = /MSIE ([\d.]+)|rv:([\d.]+)/.exec(agent);
if(exp) {
if(exp[1]) {
if(exp[1] === '7.0') {
Browser.version = "5.0-7.0";
}
else {
Browser.version = exp[1];
}
}
else {
Browser.version = exp[2];
}
}
}
if(types.edge) {
Browser.type = Browser.TYPES.EDGE;
var exp = /Edge\/([\d.]+)/.exec(agent);
if(exp) {
Browser.version = exp[1];
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment