最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Javascript与php区分移动设备和PC访问站点
时间:2015-04-20 编辑:简简单单 来源:一聚教程网
一、js实现
window.onload=function(){
var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos",
"incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire"); if (browser.indexOf(mobileAgent[i])!=-1){ isMobile = true; } }
var browser = navigator.userAgent.toLowerCase();
var isMobile = false;
for (var i=0; i
//alert(mobileAgent[i]);
location.href = 'http://www.111com.net';
break;
}
测试:http://test92.sinaapp.com/ 用PC打开时,显示Hello WAP,用移动设备访问时,将跳转www.111com.net.(可以直接在chrome中测试移动端访问站点)
二、php实现
PHP检测移动设备需要借助mobile detection类库。mobile detection 类库下载地址:https://github.com/dwqs/Mobile-Detect
Mobile_Detect.php识别手机
require_once "Mobile_Detect.php";
$detect = new Mobile_Detect;
if($detect->isMobile()) {
echo "";
}
?>
测试:http://www.111com.net/ 也可以单独对移动系统和设备进行识别
//所有平板设备
if( $detect->isTablet()) {
}
//是移动但非平板设备
if( $detect->isMobile() && !$detect->isTablet()) {
}
//IOS系统
if( $detect->isiOS()) {
}
//Android系统
if( $detect->isAndroidOS()) {
}
//WindowsPhone系统
if( $detect->isWindowsPhoneOS()) {
}