在index.html中添加
1、在页面中添加下面这段代码(移动端页面)
<script type="text/javascript">
// 有关浏览器类型的信息都藏在USER-AGENT里面,首先读取navigator.userAgent里面的信息,为了方便 利用toLowerCase方法转成小写的形式
var sUserAgent = navigator.userAgent.toLowerCase();
//调用match方法进行匹配属于哪一类浏览器,一下判断都是移动端浏览器
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) {
//本页面是移动端页面,不需要做任何操作
} else {
//如果是PC端就跳转打开PC端页面
location = 'Pc端页面地址';
}
</script>
2、在页面中添加下面这段代码(PC端页面)
<script type="text/javascript">
// 有关浏览器类型的信息都藏在USER-AGENT里面,首先读取navigator.userAgent里面的信息,为了方便利用toLowerCase方法转成小写的形式
var sUserAgent = navigator.userAgent.toLowerCase();
//调用match方法进行匹配属于哪一类浏览器,一下判断都是移动端浏览器
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) {
//如果是移动端就跳转打开移动端页面
location = '移动端页面地址';
} else {
//Pc端进入不需要其他操作
}
</script>