在智能移動終端橫行霸道的今天,使用移動終端來訪問網站的用戶是越來越多,但針對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站地址"); } ?>