js判断是否手机访问的方法就是判断navigator.userAgent中是否有手机设备的关键字
方法一:
(function(){
let reWriteUrl = function(url){
if(url){
let Splits = url.split("/"),siteName = window.location.pathname,query = window.location.search,hash = window.location.hash;
if(typeof siteName !== "undefined"){
return "/mobile"+siteName+query+hash;
}
}
};
if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){
let url = window.location.href;
let pathname = window.location.pathname;
try{
window.location.href = reWriteUrl(url);
}catch(e){}
}
})();
方法二:
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='/mobile/';
}
}
$(function(){
browserRedirect();
});