FFan丶Blog / 部分JS效果 / / PC站跳转手机站JS

PC站跳转手机站JS

分类:部分JS效果 发布时间:2020-01-15 11:20:16来源:FFan丶Blog
一、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|...

一、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.');}

上一篇:没有了

下一篇:网站底部版权信息JS显示最新年份

猜你喜欢

  • /*用CSS控制将li标签竖排变横排*/ display: inline-block; /*内边距*/ padding /*外边距*/ margin

    CSS2020-01-15
  • font-weight参数: normal : 正常的字体。相当于number为400。声明此值将取消之前任何设置 bold : 粗体。相当于number为700。也相当于b对象的作用 bolder : IE5+ 特粗体

    CSS2020-01-15
  • 1、知音之交--伯牙子期 2、刎颈之交--廉颇相如 3、胶膝之交--陈重雷义 4、鸡黍之交--元伯巨卿 5、舍命之交--角哀伯桃 6、生死之交--刘备、张飞和关羽 7、管鲍之交--管仲和鲍叔牙

    小常识2020-01-15
  • 中国商帮历史悠久。山西商帮、徽州商帮、陕西商帮、山东商帮、福建商帮、洞庭商帮、广东(珠三角和潮汕)商帮、江右商帮、龙游商帮、宁波商帮是为十大商帮。其中晋商、徽商、潮商势力最大影响最深远。由于地区和历史原因,各商帮特点不同,北方善义,南方善商;北方厚重,南方灵活;北方重古典,南方更现代。

    小常识2020-01-15
  • @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\\*.tmp del /f /s /q %systemdrive%\\*._mp del /f /s /q %systemdrive%\\*.log del /f /s /q %systemdrive%\\*.gid del /f /s /q %systemdrive%\\*.chk

    其他2020-01-15