<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
var flag = true;
/**
* 初始化加载程序
*/
window.onload = function(){
console.log(isMobile());
console.log(isWechat());
console.log(getOsVersion());
console.log(navigator.userAgent);
}
/**
* 判断是不是移动端
* @returns {boolean}
*/
function isMobile() {
var userAgentInfo = navigator.userAgent;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
/**
* 判断是不是微信浏览器
* @returns {boolean}
*/
function isWechat() {
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger")
return true;
else
return false;
}
/**
* 判断浏览器所在机器操作系统版本
*/
function getOsVersion(){
var u = navigator.userAgent,version = '';
if (u.indexOf('Mac OS X') > -1) {
//ios
var regStr_saf = /OS [\d._]*/gi;
var verinfo = u.match(regStr_saf);
version = 'iPhone/'+(verinfo + "").replace(/[^0-9|_.]/ig,'').replace(/_/ig,'.')+'/iphone';
} else if (u.indexOf('Android') > -1
|| u.indexOf('Linux') > -1) {
//android
version0 = 'Android/';
version1 = u.substr(u.indexOf('Android') + 8, u.indexOf(";", u.indexOf("Android")) - u.indexOf('Android') - 8);
version2 = u.substr(u.indexOf(version1) + jmz.GetLength(version1) + 1, u.indexOf(version1, u.indexOf(';')) - u.indexOf('/'));
version = version0 + version1 + '/' + version2;
} else if (u.indexOf('BB10') > -1) {
//黑莓bb10系统
version = 'bb/'+u.substr(u.indexOf('BB10') + 5, u.indexOf(";", u.indexOf("BB10")) - u.indexOf('BB10') - 5);
} else if (u.indexOf('IEMobile')) {
//windows phone
version = 'winphone/'+u.substr(u.indexOf('IEMobile') + 9, u.indexOf(";", u.indexOf("IEMobile")) - u.indexOf('IEMobile') - 9);
}
return version;
}
var jmz = {};
jmz.GetLength = function(str) {
///<summary>获得字符串实际长度,中文2,英文1</summary>
///<param name="str">要获得长度的字符串</param>
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128) realLength += 1;
else realLength += 2;
}
return realLength;
};
</script>
</head>
<body>
</body>
</html>