[TOC] #### 檢測當前是否為移動設備 ```javascript /** * 檢測當前是否為移動設備 * * @return true|false 是|否 */ function is_mobile() { // 其他類型的移動操作系統(tǒng)類型,自行添加 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; } } ```