2013-02-18 80 views
4

我的目標是建立一個基於Wordpress的移動網站(用於手機和平板電腦)和響應式桌面網站。我想要最簡單的方法來實現防呆設備檢測。檢測手機或平板設備

移動網站將擁有許多功能,這些功能只會真正惠及觸控設備,並將爲手機和平板電腦定製設計。桌面版網站將會完全不同(使用相同的頁面但帶有額外的內容),並且只有在任何設備漏過檢測時纔會完全響應。

我有這一個班輪,將檢測觸摸設備,並重定向到另一個頁面,但它似乎太簡單,不能成爲設備檢測的傻瓜證明方法。這有多愚蠢的證據?它適用於Windows和Android設備以及iOS嗎?

這是我第一次做這樣一個網站,不知道效果如何這將是:

<!-- Redirect to the mobile index page if we're on a touch device --> 
<script>if('ontouchstart' in window) window.location = 'mobile.html';</script> 

回答

0

該代碼會工作得很好,幾乎任何設備是觸摸啓用。

1

可以使用mdetect(移動檢測)庫來爲你做這個。它用幾種語言編寫,並會檢測Windows,Android,iOS等。請確保您閱讀文檔。

,請注意該JavaScript版本包含了這樣的警告:

// WARNING: 
// These JavaScript-based device detection features may ONLY work 
// for the newest generation of smartphones, such as the iPhone, 
// Android and Palm WebOS devices. 
// These device detection features may NOT work for older smartphones 
// which had poor support for JavaScript, including 
// older BlackBerry, PalmOS, and Windows Mobile devices. 
// Additionally, because JavaScript support is extremely poor among 
// 'feature phones', these features may not work at all on such devices. 
// For better results, consider using a server-based version of this code, 
// such as Java, APS.NET, PHP, or Ruby. 
1

對於移動偵測的PHP我總是使用$ _ SERVER [ 'HTTP_USER_AGENT' ]變量。 我使用下面的功能對移動站點(我想支持的特定設備)的獲取者進行非常精細的控制。只需在陣列中添加useragents(如'Ipad')即可添加其他設備。

function detectmobile(){ 
    $agent = $_SERVER['HTTP_USER_AGENT']; 
    $useragents = array (
     "iPhone", 
     "iPod", 
     "Android", 
     "blackberry9500", 
     "blackberry9530", 
     "blackberry9520", 
     "blackberry9550", 
     "blackberry9800", 
     "webOS" 
     ); 
     $result = false; 
    foreach ($useragents as $useragent) { 
    if (preg_match("/".$useragent."/i",$agent)){ 
      $result = true; 
     } 
    } 
return $result; 
} 
if (detectmobile() == true) { wp_redirect('http://pageyouwwanttoredirectto'); } 

用法:你可以將上面的代碼添加到你的wordpress主題的functions.php文件中。

相關問題