通过UserAgent判断进行移动适配

nginx版

PC ==>> 移动

if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)){rewrite ^/(.*)$ http://m.2dan.cc/$1 permanent;}

移动 ==>> PC

if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)){rewrite ^/(.*)$ http://www.2dan.cc.com/$1 permanent;}

js版

(function(){var ua=navigator.userAgent.toLowerCase();var bIsIpad=ua.match(/ipad/i)=="ipad";var bIsIphoneOs=ua.match(/iphone os/i)=="iphone os";var bIsAndroid=ua.match(/android/i)=="android";var bIsWM=ua.match(/windows mobile/i)=="windows mobile";
if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsWM){}else{var url=window.location.href;url=url.replace("m","www");window.location.href=url}})();

标签:移动适配, useragent

添加新评论