现在移动端那么火爆,很多人都分开两个网站,移动端和代码端,那么一定会做自动跳转,也就是手机用户访问了www的pc网站自动跳转到m的移动站,反过来也一样,那么该如果做呢?其他很简单,复制下面的代码到你对应的网页就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<script type="text/JAVAscript"> var test = yidong(); if (test == "0") { location.href = document.URL.replace("www", "m"); } function yidong() { var userAgentInfo = navigator.userAgent.toLowerCase(); var Agents = ["Android", "iphone", "symbianos", "windows phone", "ipad", "ipod"]; var flag = "1"; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = "0"; break; } } return flag; } </script> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<script type="text/JavaScript"> var test = yidong(); if (test == "0") { }else{location.href = document.URL.replace("//m", "//www");} function yidong() { var userAgentInfo = navigator.userAgent.toLowerCase(); var Agents = ["android", "iphone", "symbianos", "windows phone", "ipad", "ipod"]; var flag = "1"; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = "0"; break; } } return flag; } </script> |
还可以这么判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var browser = { versions: function () { var u = navigator.userAgent, App = navigator.appVersion; return { //移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('Khtml') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端 IOS: !!u.match(/(i[^;]+;( U;)? CPU.+mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language: (navigator.browserLanguage || navigator.language).toLowerCase() } document.writeln("语言版本: " + browser.language); document.writeln(" 是否为移动终端: " + browser.versions.mobile); document.writeln(" ios终端: " + browser.versions.ios); document.writeln(" android终端: " + browser.versions.android); document.writeln(" 是否为iPhone: " + browser.versions.iPhone); document.writeln(" 是否iPad: " + browser.versions.iPad); document.writeln(navigator.userAgent); if (browser.versions.android || browser.versions.iPhone || browser.versions.iPad) { alert("是移动端"); } else { alert("不是移动端"); } |
文章允许转载,请保留本文链接:https://www.zhe94.com/354.html