手机访问自动跳转触屏版 从触屏版访问电脑版不跳转由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“电脑百度文库变手机版”。
建站的时候建有PC版(电脑版)和触屏版(手机版),想要达到的功能是当第一次访问主页网址的时候自动判断,如果是电脑则访问PC版,如果是手机、Pad则跳转触屏版,而在手机版上点击访问电脑版时则直接访问电脑版(此时不再跳转成触屏版)。可以通过以下代码实现∶
A、在主页调用的js文件末尾加上以下代码
var getParam = function(name){ var search = document.location.search;var pattern = new RegExp(“[?&]”+name+“=([^&]+)”, “g”);var matcher = pattern.exec(search);var items = null;if(null!= matcher){ try{ items = decodeURIComponent(decodeURIComponent(matcher[1]));}catch(e){ try{ items = decodeURIComponent(matcher[1]);}catch(e){ items = matcher[1];} } } return items;};pc=getParam('pc');/*以上是获取url参数pc的值,包括null*/ /*alert(pc);*/
/*alert(getParam('pc'));*/
var is_iPd = navigator.userAgent.match(/(iPad|iPod|iPhone)/i)!= null;var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|win ce)/i)!= null;if((is_mobi && window.location.search.indexOf(“mv=fp”)
B、在触屏版的footer代码中加上以下代码
电脑版
通过pc变量来判断用户是初始访问网址还是通过触屏版访问网址。