javascript自动识别是否移动设备访问

代码

JavaScript | 复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function is_pc(){
    var os = new Array("Android","iPhone","Windows Phone","iPod","BlackBerry","MeeGo","SymbianOS");  // 其他类型的移动操作系统类型,自行添加
    var info = navigator.userAgent;
    var len = os.length;
    for (var i = 0; i < len; i++) {
        if (info.indexOf(os[i]) > 0){
            return false
        }
    }
    return true;
}
 
// 如果是移动设备就直接跳转到手机网站页面
if(!is_pc()){
    window.location.href="手机页面链接地址";
}

还有一种:

JavaScript | 复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var isMobile = {  
    Android: function() {  
        return navigator.userAgent.match(/Android/i) ? true false;  
    },  
    BlackBerry: function() {  
        return navigator.userAgent.match(/BlackBerry/i) ? true false;  
    },  
    iOS: function() {  
        return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true false;  
    },  
    Windows: function() {  
        return navigator.userAgent.match(/IEMobile/i) ? true false;  
    },  
    any: function() {  
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());  
    }
原文地址:https://www.cnblogs.com/lzq198754/p/5780262.html