一、PC站所有页面跳转同一手机页面
function is_mobile() {
var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;var u = navigator.userAgent;
var result = regex_match.exec(u);
if (null == result) {
return false
} else {
return true
}
}
if (is_mobile()) {
window.location.href = "http://手机站域名/?" + window.location.href;}
二、PC站页面跳转手机站页面一一对应
写法1:
var s=document.referrer;
var browser_class = navigator.userAgent;
var browser_class_name1 = browser_class.match("Mobile");var browser_class_name2 = browser_class.match("mobile");var location_url = window.location.href;
if (browser_class_name1 != null || browser_class_name2 != null){if (location_url.match("m\\.") == null){
var newhref = location_url.replace("www","m");window.location.href= newhref;
}
}
写法2:
function is_mobile() {
var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;var u = navigator.userAgent;
var result = regex_match.exec(u);
if (null == result) {
return false
} else {
return true
}
}
if (is_mobile()) {
var s = window.location.href;
ss = s.split("/");
if (ss[3] == null || ss[3] == "") {
window.location.href = "http://手机站域名/";
} else if (ss[4] == null || ss[4] == "") {
window.location.href = "http://手机站域名/" + ss[3] + "/";} else {
window.location.href = "http://手机站域名/" + ss[3] + "/" + ss[4];}
}
三、PC站与手机站相互跳转对应页面
写法1:
var isPC = function () {
var userAgentInfo = navigator.userAgent.toLowerCase();var Agents = new Array("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;
}
var pcUlr = "http://PC站域名";
var mobleUlr = "http://手机站域名";
if (isPC() && window.location.href.indexOf(mobleUlr) != -1) {window.location.href = pcUlr + GetUrlRelativePath()} else if (!isPC() && window.location.href.indexOf(pcUlr) != -1) {window.location.href = mobleUlr + GetUrlRelativePath()}
function GetUrlRelativePath() {
var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(start);
if (relUrl.indexOf("?") != -1) {
relUrl = relUrl.split("?")[0];
}
return relUrl;
}
写法2:
PC端:if((navigator.userAgent.match(/(phone|pad|pod|mobile|ios|android|BlackBerry|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|symbian|transcoder)/i))){window.location = window.location.href.replace(/www./, 'm.');}
手机端:if(!(navigator.userAgent.match(/(phone|pad|pod|mobile|ios|android|BlackBerry|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|symbian|transcoder)/i)) && window.location.href.match(/(http[s]*:\/\/)m(ip)*\./)){window.location.href=window.location.href.replace(/(http[s]*:\/\/)m(ip)*\./, '$1www.');}
如果文章对你有帮助,欢迎点击上方按钮打赏作者
版权声明:若无特殊注明,本文皆为( FFan )原创,转载请保留文章出处。