layui 中的终端/设备检测代码
时间:2023-3-17 16:02 作者:wen 分类: F2E
// 设备信息
function device(key) {
var agent = navigator.userAgent.toLowerCase(); // 获取 ua ①
// 获取版本号
var getVersion = function (label) {
var exp = new RegExp(label + '/([\^\\s\\_\\-]+)');
label = (agent.match(exp) || [])[1];
return label || false;
}
// 返回结果集
var result = {
os: function () { // 底层操作系统 ②
if (/windows/.test(agent)) {
return 'windows';
} else if (/linux/.test(agent)) {
return 'linux';
} else if (/iphone|ipod|ipad|ios/.test(agent)) {
return 'ios';
} else if (/mac/.test(agent)) {
return 'mac';
}
}(),
ie: function () { // ie 版本 ③
return (!!window.ActiveXObject || "ActiveXObject" in window) ? (
(agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于 ie11 并没有 msie 的标识
) : false;
}(),
weixin: getVersion('micromessenger') // 是否是微信 ④
};
// 任意的 key
if (key && !result[key]) {
result[key] = getVersion(key);
}
// 移动设备
result.android = /android/.test(agent);
result.ios = result.os === 'ios';
result.mobile = (result.android || result.ios) ? true : false;
return result;
}
检测是否是手机终端
// 检测终端环境是否是手机
function device() {
var agent = navigator.userAgent.toLowerCase(); // 获取 ua ①
// 移动设备
let result = /android|iphone|ipod|ipad|ios/.test(agent);
if (result) {
return true;
} else {
return false;
}
}
标签: javascript