[TOC] #### 检测当前是否为移动设备 ```javascript /** * 检测当前是否为移动设备 * * @return true|false 是|否 */ function is_mobile() { // 其他类型的移动操作系统类型,自行添加 const os = ["Android", "iPhone", "Windows Phone", "iPod", "BlackBerry", "MeeGo", "SymbianOS"]; const info = navigator.userAgent; for (let i = 0; i < os.length; i++) { if (info.indexOf(os[i]) > 0) { return true; } } return false; } ``` #### 检测是否为 ios 移动设备 ```javascript /** * 检测是否为 ios 移动设备 */ function isIos() { const u = navigator.userAgent; if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) { return true; } return false; } ``` ```javascript function isIOS() { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 if (isAndroid) return false; if (isIOS) return true; } ``` #### 检测是否为 Android 设备 ```javascript function isAndroid() { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 if (isIOS) return false; if (isAndroid) return true; } ``` #### 检测当前是否为移动设备 以下代码来自穿越火线官方活动页面代码 ```javascript function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp = sUserAgent.match(/midp/i) == "midp"; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; // 如果是移动设备,进行调整到适用于移动端设备的页面 if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { window.location.href = "https://app.daoju.qq.com/act/a20240925pass/index.html" + location.search; } } ```