2011-11-25 65 views
0

我的问题是如何在jQuery中检测到客户端是否正在从移动设备中看到网站。这包括任何便携式设备(手机,i-pad,i-phone,adroid,windows等)jquery中的便携式设备检测

谢谢你的时间。

+0

可能重复[最好的方式来检测手持设备中的jQuery(http://stackoverflow.com/questions/3514784/best-way-to-detect-handheld-device-in -jquery) – tzot

+0

http://stackoverflow.com/questions/3514784/best-way-to-detect-handheld-device-in-jquery Modernizr对于大多数情况也是一个很好的解决方案 –

回答

0

其实你可以在普通的javscript中嗅探用户代理,jQuery建议要做的一件事情不是检查userAgent,而是应该检查受支持的功能。对于这个jQuery提供的jQuery.support

var deviceIphone = "iphone"; 
var deviceIpod = "ipod"; 

//Initialize our user agent string to lower case. 
var uagent = navigator.userAgent.toLowerCase(); 

//************************** 
// Detects if the current device is an iPhone. 
function DetectIphone() 
{ 
    if (uagent.search(deviceIphone) > -1) 
     return true; 
    else 
     return false; 
} 

//************************** 
// Detects if the current device is an iPod Touch. 
function DetectIpod() 
{ 
    if (uagent.search(deviceIpod) > -1) 
     return true; 
    else 
     return false; 
} 

//************************** 
// Detects if the current device is an iPhone or iPod Touch. 
function DetectIphoneOrIpod() 
{ 
    if (DetectIphone()) 
     return true; 
    else if (DetectIpod()) 
     return true; 
    else 
     return false; 
}