检测 iPhone 浏览器
是否有脚本可以检测访问者是否使用 iPhone(无论是什么浏览器,可能是 iPhone Safari、iPhone for Opera 等)?
然后将关闭一些我的 JavaScript。
谢谢...
is there a script to detect, if the visitor use iphone (whatever it's browser, may iphone Safari, iPhone for Opera or etc.)?
Then will shutdown some some of my JavaScript.
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在网上搜索有两种常见的方法可以实现这一点。
我最喜欢的是 PHP,它是如此干净?哇。 :D
在 PHP 中你可以编写
在 javascript 中你可以编写
希望有帮助。
PK
searching on the net there are two common ways of achieving this.
My favorite though is in PHP its just so clean? wow. :D
In PHP you can write
and in javascript you can write
Hope that helps.
PK
传统观点认为 iOS 设备有一个 Safari 用户代理和一个 UIWebView 用户代理。这种假设是不正确的,因为 iOS 应用程序可以并且确实自定义其用户代理。这里的主要罪犯是Facebook。
比较 iOS 设备中的这些用户代理字符串:
请注意,在 iPad 上,Facebook UIWebView 的用户代理字符串包括“iPhone”。
在 JavaScript 中识别 iPhone 的旧方法:
如果您采用这种方法来检测 iPhone,那么如果用户来自 iPad 上的 Facebook,您最终会得到 IS_IPHONE 为 true 的结果。这可能会产生一些奇怪的行为!
在 JavaScript 中识别 iPhone 的正确方法:
我们声明 IS_IPHONE 在 iPad 上为 false,以掩盖奇怪的 Facebook UIWebView iPad 用户代理。这是用户代理嗅探如何不可靠的一个例子。自定义用户代理的 iOS 应用程序越多,用户代理嗅探的问题就越多。如果您可以避免用户代理嗅探(提示:CSS 媒体查询),那就这样做。
The conventional wisdom is that iOS devices have a user agent for Safari and a user agent for the UIWebView. This assumption is incorrect as iOS apps can and do customize their user agent. The main offender here is Facebook.
Compare these user agent strings from iOS devices:
Note that on the iPad, the Facebook UIWebView's user agent string includes 'iPhone'.
The old way to identify iPhone in JavaScript:
If you were to go with this approach for detecting iPhone, you would end up with IS_IPHONE being true if a user comes from Facebook on an iPad. That could create some odd behavior!
The correct way to identify iPhone in JavaScript:
We declare IS_IPHONE to be false on iPads to cover for the bizarre Facebook UIWebView iPad user agent. This is one example of how user agent sniffing is unreliable. The more iOS apps that customize their user agent, the more issues user agent sniffing will have. If you can avoid user agent sniffing (hint: CSS Media Queries), DO IT.
该脚本在 userAgent 中检查 iPhone 或 iPod,然后执行操作。尝试一下。
This script checks for iPhone or iPod in the userAgent and then executes an action. Give this a try.
虽然我喜欢这里的答案,但我认为最好使用以下内容...
http://www.htaccesstools.com/articles/detect-and-redirect-iphone/
或者
这是一个 .htaccess 替代方案,这意味着给您留下更多空间来处理 php :) 希望它有所帮助
Although I like the answers here, I think its better to use the following ...
http://www.htaccesstools.com/articles/detect-and-redirect-iphone/
OR
This is a .htaccess alternative, which means leaves you more room to deal with php :) hope it helps
迟到了,间接回答这个问题,但我发现它很有用。
不要重定向移动用户,而是考虑使用响应式 CSS 设计为桌面和移动用户提供具有两种不同样式的相同内容。这有助于避免拆分和重复代码。您可以从头开始编写响应式 CSS 代码,或使用 Twitter Bootstrap 等预打包框架。
Late, indirect answer to this question, but I found it useful.
Instead of redirecting mobile users, consider using responsive CSS design to deliver desktop and mobile users the same content with two different styles. This helps avoid splitting and duplicating code. You can code responsive CSS from scratch, or use prepackaged frameworks like Twitter Bootstrap.