JS

js 判断手机,pc 实现pc 手机端跳转

 
js 判断手机,pc 实现pc 手机端跳转: 
前提:手机站和PC站URL一一对应:
 
http://www.fllo.cn/ -> http://www.fllo.cn/
http://www.fllo.cn/society/201708/2377.html -> http://m.fllo.cn/society/201708/2377.html

判断js代码如下(手机站中添加):
 
(function(){
var locatHref = location.href;
if(IsPC() == true){
location.href = locatHref.replace('m.fllo.cn', 'www.fllo.cn');
}

})();
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}

PC站中添加:
 
(function(){
var locatHref = location.href;
if(IsPC() == false){
location.href = locatHref.replace('www.fllo.cn', 'm.fllo.cn');
}

})();
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}

演示效果见:
 
http://www.fllo.cn/
 
http://m.fllo.cn/

0 个评论

要回复文章请先登录注册