使用JavaScript或PHP进行iPad检测

iPad

The hottest device out there right now seems to be the iPad. iPad this, iPad that, iPod your mom. I'm underwhelmed with the device but that doesn't mean I shouldn't try to account for such devices on the websites I create. In Apple's developer tip sheet they provide the iPad's user agent string:

目前最热的设备似乎是iPad。 iPad这,iPad那,iPod你的妈妈。 我对设备不满意,但这并不意味着我不应该在自己创建的网站上考虑此类设备。 在Apple的开发人员提示中,他们提供了iPad的用户代理字符串:


Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10


Given that string we can create a few code snippets to determine if the user is being a smug, iPad-using bastard.

给定该字符串,我们可以创建一些代码段,以确定用户是否是自欺欺人,使用iPad的混蛋。

JavaScript (The JavaScript)


// For use within normal web clients 
var isiPad = navigator.userAgent.match(/iPad/i) != null;

// For use within iPad developer UIWebView
// Thanks to Andrew Hedges!
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);


A quick String.match regular expression test can check for the presence of "iPad" in the user agent string.

快速的String.match正则表达式测试可以检查用户代理字符串中是否存在“ iPad”。

PHP (The PHP)


$isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');


This time we look for the position of "iPad" in the user agent string.

这次,我们在用户代理字符串中寻找“ iPad”的位置。

.htaccess (The .htaccess)


RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]


Using some logic from Drew Douglass' excellent mobile redirection post, we can redirect users to a mobile version of your website if you so desire.

使用Drew Douglass出色的移动重定向文章中的一些逻辑,如果您愿意,我们可以将用户重定向到您网站的移动版本。

So what would you the above tests for? You may want to redirect iPad users to a different version of your website. You may want to implement different styles to your standard website if your user is surfing on an iPad.

那么您对以上测试的目的是什么? 您可能需要将iPad用户重定向到您网站的其他版本。 如果您的用户在iPad上冲浪,则可能要对标准网站实施不同的样式。

翻译自: https://davidwalsh.name/detect-ipad

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值