第一、
使用js让广告适配合适的设备(当然你也可以直接让css来隐藏掉广告,但是js可以让网页的速度快一点,因为不需要去加载web端的广告了)
function is_mobile() {
var regex_match = /(nokia|iphone|android|motorola|^|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|^|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^|longcos|pantech|gionee|^|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operasmobi|operamini|320x320|240x320|176x220)/i;
var u = navigator.userAgent;
if (null == u) { return true;}
var result = regex_match.exec(u);
if (null == result) {
return false
} else {
return true
}
}
用法:
if (is_mobile()) {
/* 手机端的代码; */
}else{
/* web端的内容; */
}
第二、
通过服务器端配合前端来适配手机端,这样做对技术能力有一定的要求,但是会让你的网站变得焕然一新,这里以php为例:
1、使用服务器来抓去浏览器头,检查是否是手机端,下面给一个不错的函数
function is_mobile(){
/* returns true if one of the specified mobile browsers is detected */
$regex_match="/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
用法:
if( is_mobile()){
/* 手机端的内容,如果不想手机端显示,那么这里留空即可,你可以可以设置只在手机端显示的内容 */
}else{
/* web端的内容,如果你只是想web端才能访问,那么在这里写如前端输出的内容 */
}
在服务器端,把web端才显示的内容都放进else里面,这样当手机端打开网站的时候,就不会显示凌乱了。
2、通过之前说的前端的方法进行设置即可。