主要通过 navigator.userAgent
来获取设备信息,然后进行判断:
function IsPC() {
var userAgentInfo = navigator.userAgent; //获取终端系统以及浏览器相关信息
// 手机类型
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true; //设置标志,默认是 PC 端
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
// 如果是移动端,就修改标志位,并且结束判断
flag = false;
break;
}
}
return flag;
}
var flag = IsPC(); //true为PC端,false为手机端
各 PC 端浏览器的信息罗列如下,以后如需对浏览器进行区别,可直接对其关键字段进行判断即可:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko