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显示最新年份

猜你喜欢

  • 文章列表 文章标题:<!--{$article_list.title}--> 文章链接:<!--{$article_list.url}--> 图片属性:pic="true" 随机排序:orderby="rand" 行号 (从1开始):<!--{$article_list.rownum}--> 

    ICMS2020-01-15
  • .pagelist a,.pagelist span {border-style: solid;border-width: 1px;border-color: #e91e63;margin: 5px;padding: 4px 10px;} .pagelist span.page_nowindex {background-color: #e91e63;}

    ICMS2020-01-15
  • 获取 10个分类下 每个分类最新的10篇文章<!--{iCMS:category:list loop="true" row="10"}--> <a href="<!--{$category_list.url}-->"><!--{$category_list.title}--></a>  <!--{iCMS:article:list loop="true" row="10" cid="$category_list.cid"}-->    <a href="<!--{$article_list.url}-->&quo

    ICMS2020-01-20
  • 一般的文字截断(适用于内联与块):.text-overflow {     display:block;/*内联对象需加*/     width:31em;/*指定宽度*/     word-break:keep-all;/* 不换行 */

    CSS2020-04-13
  •     <!-- 声明文档使用的字符编码 -->     <meta charset='utf-8'>     <!-- 优先使用 IE 最新版本和 Chrome -->     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>

    html相关标签2020-07-06