可以判断手机等移动客户端浏览网站跳转到WAP站的JS代码来源:巨卓腾网络 | 时间:2012-11-07 12:07:07随着手机功能的不断发展,越来越多的人习惯直接用手机等移动客户端浏览网页。所以在网站建站中,越来越多的网站都建立了一个专门针对手机等移动客户端的网站,那如何让通过手机访问网站的时候,自动调整到移动客户端网站呢? 下面把代码奉上: function checkMobile(){ var isiPad = navigator.userAgent.match(/iPad/i) != null; if(isiPad){ return false; } var isMobile=navigator.userAgent.match( /iphone|android|phone|mobile|wap|netfront|x11|java|operamobi|operamini|ucweb| windowsce|symbian|symbianos|series|webos|sony|blackberry|dopod|nokia| samsung|palmsource|xda|pieplus|meizu|midp|cldc|motorola|foma|docomo| up.browser|up.link|blazer|helio|hosin|huawei|novarra|coolpad|webos|techfaith| palmsource|alcatel|amoi|ktouch|nexian|ericsson|philips|sagem|wellcom| bunjalloo|maui|smartphone|iemobile|spice|bird|zte-|longcos|pantech|gionee| portalmmm|jig browser|hiptop|benq|haier|^lct|320x320|240x320|176x220/i)!= null; if(isMobile){ return true; } return false; } function _getCookie(cname){ var cookieStr = document.cookie.match("(?:^|;)\\s*" + cname + "=([^;]*)"); return cookieStr ? unescape(cookieStr[1]) : ""; } var URL_MAP = [ ["invogue/style-pk/", "invogue/"], ["invogue/dream-ticket/", "invogue/"], ["invogue/high-street/", "invogue/"], ["invogue/vogue-style/", "invogue/"], ["invogue/dress-q/", "invogue/"], ["invogue/accessory/", "invogue/"], ["invogue/brand-journey/", "invogue/"], ["invogue/industry/", "invogue/"], ["invogue/brand-news/", "invogue/"], ["invogue/vogue-office/", "invogue/"], ["invogue/street-chic/", "street-chic/"], ["invogue/image-mania/", "image-mania/"], ["beauty/makeup/", "beauty/"], ["beauty/new-in-store/", "beauty/"], ["beauty/howto/", "beauty/"], ["beauty/celeb-beauty/", "beauty/"], ["beauty/skincare/", "beauty/"], ["beauty/fitness/", "beauty/"], ["beauty/hair/", "beauty/"], ["beauty/editor-pick/", "beauty/"], ["beauty/brand-news/", "beauty/"], ["beauty/fragrance/", "beauty/"], ["beauty/street-styling/", "street-chic/"], ["people/celeb-style/", "people/"], ["people/best-worst/", "people/"], ["people/party/", "people/"], ["people/icons/", "people/"], ["people/red-carpet/", "people/"], ["people/talk-of-town/", "people/"], ["people/models/", "people/"], ["people/star-spot/", "street-chic/"], ["shoes-bags/", "shoes-bags/"], ["jewelry-watch/season-jewelry/", "jewelry/"], ["jewelry-watch/season-watch/", "watch/"], ["magazine/current-issue/", "magazine/"] ]; function _getCookie(cname){ var cookieStr = document.cookie.match("(?:^|;)\\s*" + cname + "=([^;]*)"); return cookieStr ? unescape(cookieStr[1]) : ""; } (function(){ if(checkMobile()){ if( _getCookie("visitWWW")!=="visited" ){ var thisHost = "http://%22+location.hostname/; var thisHREF = document.URL, request_uri = thisHREF.substr(thisHost.length+1), reg; if( thisHREF=="http://www."||thisHREF=="http://www./"|| ) { window.location.href="http://m./"; }else{ for(var i=0; i<URL_MAP.length-1; i++){ reg = new RegExp("^" + URL_MAP[i][0], "i"); console.log(reg.test(request_uri)) if(reg.test(request_uri)){ request_uri = request_uri.replace(URL_MAP[i][0], URL_MAP[i][1]); window.location.href = "http://m./" + request_uri; } } } } } })(); 将上述代码复制到js文件中,并将js文件命名为mobile.js。在官网全部页面的<title>标签之下,加上一句js调用代码,代码如下: <!--手机端跳转--> 注:以上红色字体部分,请按照自己的域名url和js路径更改。如有问题,可与我们联系。 点击下载js文件:mobile.js 本文版权归原作者所有,转载请注明原文来源出处,巨卓腾网络感谢您的支持! |
|