随着乔布斯的回归,iPad2的发布,看来移动端的开发话题越来越火热了。在此列出一些能够在iOS的最大竞争者——安卓(Android)系统的检测方法。
JavaScript判断方法
搜索user agent字符串中的Android单词是最省事儿的方法:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span>(navigator.userAgent.match(/Android/i)) { </span></span></li> <li> <span> </span><span class="comment">// Do something! </span><span> </span> </li> <li class="alt"> <span> </span><span class="comment">// Redirect to Android-site? </span><span> </span> </li> <li> <span> window.location = </span><span class="string">'http://android.davidwalsh.name'</span><span>; </span> </li> <li class="alt"><span> } </span></li> </ol>
PHP判断方法
同样,我们可以在PHP中使用strstr方法搜索user agent中是否有Android:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">if</span><span>(</span><span class="func">strstr</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">'HTTP_USER_AGENT'</span><span>],</span><span class="string">'Android'</span><span>)) { </span></span></li> <li> <span> header(</span><span class="string">'Location: http://android.davidwalsh.name'</span><span>); </span> </li> <li class="alt"> <span> </span><span class="func">exit</span><span>(); </span> </li> <li><span> } </span></li> </ol>
另外,可以通过.htaccess来判断
我们可以使用.htaccess来判断和响应安卓设备!
<ol class="dp-xml"> <li class="alt"><span><span>RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ </span></span></li> <li> <span> RewriteRule ^(.*)$ http://android.davidwalsh.name [</span><span class="attribute">R</span><span>=</span><span class="attribute-value">301</span><span>] </span> </li> </ol>
这样你就掌握了全部三种安卓设备的检测方法。
http://article.yeeyan.org/view/56089/176760