Skip to content

Commit

Permalink
Update 浏览器判断.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 authored Apr 8, 2019
1 parent 705fe18 commit b3fccb8
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions 浏览器判断.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,113 @@ function getBrowserVersion(){
return 'Unkonwn Version';
}
getBrowserVersion();

var Browser = (function(window) {
var agent = window.navigator.userAgent;
const IE10 = 'IE10'; //IE10及以下
const IE11 = 'IE11';
const Edge = 'Edge';
const Opera = 'Opera';
const Chrome = 'Chrome';
const Firefox = 'Firefox';
const Safari = 'Safari';
var System = {
type: '',
version: '',
getIe10Version: function() {
try {
return agent.match(/MSIE ([\d.]+)/)[1] || '0';
} catch (e) {
console.log(e);
return '0';
}
},
operaVersion: function() {
try {
if (agent.indexOf('Opera') > -1) {
return agent.match(/Opera.([\d.]+)/)[1];
} else {
return agent.match(/OPR\/([\d.]+)/)[1];
}
} catch (e) {
console.log(e);
return 0;
}
},
//描述:version过滤.如31.0.252.152 只保留31.0
versionFilter: function() {
if (arguments.length === 1 && typeof arguments[0] === 'string') {
var version = arguments[0];
var start, end;
start = version.indexOf('.');
if (start > 0) {
end = version.indexOf('.', start + 1);
if (end !== -1) {
return version.substr(0, end);
}
}
return version;
} else if (arguments.length === 1) {
return arguments[0];
}
return 0;
},
} as any;

try {
// 检测浏览器类型
if (/MSIE/.test(agent)) {
System.type = IE10;
} else if (/rv:([\d.]+)\) like gecko/.test(agent.toLowerCase())) {
System.type = IE11;
} else if (agent.indexOf('Edge') > -1) {
System.type = Edge;
} else if (agent.indexOf('Opera') > -1 || agent.indexOf('OPR') > -1) {
System.type = Opera;
} else if (agent.indexOf('Chrome') > -1 && agent.indexOf('Safari') > -1) {
System.type = Chrome;
} else if (agent.indexOf('Safari') > -1 && agent.indexOf('Chrome') === -1) {
System.type = Safari;
} else if (agent.indexOf('Firefox') > -1) {
System.type = Firefox;
} else {
System.type = 'unknow';
}
// 版本号
switch (System.type) {
case IE10:
System.version = System.getIe10Version();
break;
case IE11:
System.version = '11';
break;
case Edge:
System.version = 'edge';
break;
case Firefox:
System.version = agent.match(/Firefox\/([\d.]+)/)[1];
break;
case Chrome:
System.version = agent.match(/Chrome\/([\d.]+)/)[1];
break;
case Opera:
System.version = System.operaVersion();
break;
case Safari:
System.version = agent.match(/Version\/([\d.]+)/)[1];
break;
default:
System.version = '0';
}
System.version = System.versionFilter(System.version);
//不支持的浏览器
if (System.type === IE10) {
alert(
'Sorry, your current browser version is low, you can use IE11 or higher version of the browser, in order to get a better experience,' +
' we strongly recommend that you use Chrome Browser'
);
}
} catch (e) {
console.log(e);
}
})(window);

0 comments on commit b3fccb8

Please sign in to comment.