在智能移动终端横行霸道的今天,使用移动终端来访问网站的用户是越来越多,但针对PC用户开发的网站,在移动终端上的体验非常差,这不,我们开始针对移动终端也制作了体验相对更好的页面,那么我们怎么才能知道用户使用的是哪种终端来访问我们的网站呢,总不能让用户再来记一遍我们的手机站域名吧,查阅资料,有很多方法可以实现这个需求,现在将发现的方法记录如下:
JS实现方法:
方法一:
function mobile_device_detect(url)
{
var thisOS=navigator.platform;
var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
for(var i=0;i<os.length;i++)
{
if(thisOS.match(os[i]))
{
window.location=url;
}
}
//因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
if(navigator.platform.indexOf('iPad') != -1)
{
window.location=url;
}
//做这一部分是因为Android手机的内核也是Linux
//但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
var check = navigator.appVersion;
if( check.match(/linux/i) )
{
//X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
if(check.match(/mobile/i) || check.match(/X11/i))
{
window.location=url;
}
}
//类in_array函数
Array.prototype.in_array = function(e)
{
for(i=0;i<this.length;i++)
{
if(this[i] == e)
return true;
}
return false;
}
}
mobile_device_detect("手机站地址");
方法二:
try {
var urlhash = window.location.hash;
if (!urlhash.match("fromapp"))
{
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)))
{
window.location="http://m.16css.com/";
}
}
}
catch(err)
{
}
方法三:
// JavaScript Document
function urlredirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
// PC跳转移动端
var thisUrl = window.location.href;
window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf('/')+1)+'mobile/';
}
}
urlredirect();
php实现方法:
<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
if(
strpos($agent,"comFront")
|| strpos($agent,"iPhone")//iPhone
|| strpos($agent,"MIDP")//JAVA
|| strpos($agent,"Opera Mini")//Opera for mobile
|| strpos($agent,"UCWEB")//UC Mobile Limited
|| strpos($agent,"Android")//android
|| strpos($agent,"Windows CE")//Win CE
|| strpos($agent,"Windows mobile")//Win phone
|| strpos($agent,"SymbianOS"))//Symbian
{
header("Location:手机站地址");
}else {
header("Location:PC站地址");
}
?>
本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:
原文出处:Yiiven https://cloud.tencent.com/developer/article/2193052
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有