判断是PC还是手机
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ['Android', 'iPhone',
'SymbianOS', 'Windows Phone',
'iPad', 'iPod'
];
var flag = true;
for (var i = 0; i < Agents.length; i++) {
if (userAgentInfo.indexOf(Agents[i]) != -1) {
flag = false;
break;
}
}
return flag;
}
if (IsPC()) {
alert('这是PC');
} else {
alert('这不是PC');
}
判断是否是IE
function isIE() {
if (!!window.ActiveXObject || "ActiveXObject" in window){
return true;
} else{
return false;
}
}
if (isIE()) {
alert('是IE');
} else {
alert('不是IE');
}
判断IE浏览器版本
function IEVersion() {
var userAgent = navigator.userAgent;
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE;
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
if(isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7) {
return 7;
} else if(fIEVersion == 8) {
return 8;
} else if(fIEVersion == 9) {
return 9;
} else if(fIEVersion == 10) {
return 10;
} else {
return 6;
}
} else if(isEdge) {
return 'edge';
} else if(isIE11) {
return 11;
}else{
return -1;
}
}
alert(IEVersion());
判断是否是微信的浏览器
function isWeiXin(){
var ua = window.navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i) == 'micromessenger'){
return true;
}else{
return false;
}
}
if(isWeiXin()){
alert('是微信');
}else{
alert('不是微信');
}
判断是苹果还是安卓
方法一
function detect(){
var equipmentType = "";
var agent = navigator.userAgent.toLowerCase();
var android = agent.indexOf("android");
var iphone = agent.indexOf("iphone");
var ipad = agent.indexOf("ipad");
if(android != -1){
equipmentType = "android";
}
if(iphone != -1 || ipad != -1){
equipmentType = "ios";
}
return equipmentType;
}
$('button').click(function(){
var obj=detect();
if (obj=="android") {
alert('这是安卓系统');
}else if(obj=="ios"){
alert('这是苹果系统');
}else{
alert('啥也不是');
}
});
方法二
var isMobile = {
Android: function () {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function () {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i);
},
any: function () {
return (isMobile.Android() ||
isMobile.BlackBerry() ||
isMobile.iOS() ||
isMobile.Opera() ||
isMobile.Windows());
}
};
$('#btn').click(function(){
console.log(isMobile.any());
if (isMobile.any()=='Android') {
alert('这是安卓');
}if(isMobile.any()=='iPhone') {
console.log(isMobile.any());
alert(isMobile.any());
}
});
浏览器检测汇总
var uesrBrowser = {
userAgentInfo : window.navigator.userAgent.toLowerCase(),
isPC: function() {
var ua = navigator.userAgent;
var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
var flag = true;
for (var i = 0; i < Agents.length; i++) {
if (ua.indexOf(Agents[i]) != -1) {
flag = false;
break;
}
}
return flag;
},
isIE: function() {
if (uesrBrowser.userAgentInfo.indexOf('edge/') > 0) {
return true;
}
if (!!window.ActiveXObject || "ActiveXObject" in window){
return true;
} else{
return false;
}
},
isWX: function() {
if(uesrBrowser.userAgentInfo.match(/MicroMessenger/i) == 'micromessenger'){
return true;
}else{
return false;
}
},
isIOS: function() {
var iphone = uesrBrowser.userAgentInfo.indexOf("iphone");
var ipad = uesrBrowser.userAgentInfo.indexOf("ipad");
if (iphone != -1 || ipad != -1) {
return true;
} else {
return false;
}
}
};
switch(true){
case uesrBrowser.isPC():
console.log('是PC');
if (uesrBrowser.isIE()) {
alert('IE');
}
break;
default:
if (uesrBrowser.isIOS()) {
if (uesrBrowser.isWX()) {
alert('微信');
} else {
alert('这是safari!');
}
} else {
alert('这是安卓!');
}
}