是否有一种脚本可以检测访问者是否使用iPhone(无论是使用哪种浏览器,如iPhone Safari、iPhone for Opera等)?
然后将关闭我部分的JavaScript。
谢谢...
是否有一种脚本可以检测访问者是否使用iPhone(无论是使用哪种浏览器,如iPhone Safari、iPhone for Opera等)?
然后将关闭我部分的JavaScript。
谢谢...
在网上搜索,有两种常见的实现方法。
但我最喜欢的是在PHP中,它非常简洁,太棒了 :D
在PHP中,您可以编写以下代码:
<?php
function isIphone($user_agent=NULL) {
if(!isset($user_agent)) {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}
return (strpos($user_agent, 'iPhone') !== FALSE);
}
if(isIphone()) {
header('Location: http://www.yourwebsite.com/phone');
exit();
}
// ...THE REST OF YOUR CODE HERE
?>
在 JavaScript 中,你可以编写
var agent = navigator.userAgent;
var isIphone = ((agent.indexOf('iPhone') != -1) || (agent.indexOf('iPod') != -1)) ;
if (isIphone) {
window.location.href = 'http://www.yourwebsite.com/phone';
}
希望这能有所帮助。
PK
# iOS Safari
iPad: Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3
iPhone: Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
# UIWebView
iPad: Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/98176
iPhone: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/8B117
# Facebook UIWebView
iPad: Mozilla/5.0 (iPad; U; CPU iPhone OS 5_1_1 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.1.1;FBBV/4110.0;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/5.1.1;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US;FBSF/1.0]
iPhone: Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; ru_RU) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.1;FBBV/4100.0;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.1.1;FBSS/2; tablet;FBLC/en_US]
IS_IPHONE = navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null);
如果您采用这种方法来检测iPhone,那么如果用户在iPad上通过Facebook访问,IS_IPHONE将变为true。这可能会导致一些奇怪的行为!
在JavaScript中正确识别iPhone的方法:
IS_IPAD = navigator.userAgent.match(/iPad/i) != null;
IS_IPHONE = (navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null);
if (IS_IPAD) {
IS_IPHONE = false;
}
我们在iPad上将IS_IPHONE声明为false,以应对Facebook UIWebView iPad用户代理的奇怪行为。这是用户代理嗅探不可靠的一个示例。越多的iOS应用程序自定义其用户代理,用户代理嗅探就会遇到更多问题。如果您可以避免使用用户代理嗅探(提示:CSS Media Queries),那么请尽量避免。
//Detect special conditions devices
$iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
//do something with this information
if( $iPod || $iPhone ){
//browser reported as an iPhone/iPod touch -- do something here
}else if($iPad){
//browser reported as an iPad -- do something here
}else if($Android){
//browser reported as an Android device -- do something here
}else if($webOS){
//browser reported as a webOS device -- do something here
}
<script language="javascript" type="text/javascript">
//This redirects iPhone users to the iPhone-friendly site
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "http://i.yoursite.com";
}
这个脚本检查userAgent中是否有iPhone或iPod,然后执行一个操作。试试看吧。
虽然我喜欢这里的答案,但我认为最好使用以下方法...
http://www.htaccesstools.com/articles/detect-and-redirect-iphone/
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://iphone.example.com/ [R]
或者
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{REQUEST_URI} !^/my-iPhone-site/
RewriteRule .* /my-iPhone-site/ [R]
agent.indexOf('iPod')
,不应该是原来的代码。 - mpdonadio