2011-11-25 67 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; 
}