Monday, April 2, 2012

Detect smartphone and pc device on Javascript

<html>
<body>
<script type="text/javascript"> 
var deviceIphone = "iphone";
var deviceIpod = "ipod";
var devicePc="windows";
//Initialize our user agent string to lower case.
var uagent = navigator.userAgent.toLowerCase();
//**************************
// Detects if the current device is an Window.
function DetectPc(){
    if(uagent.search(devicePc)>-1){
        return true;
    }else{
        return false;
    }
}
//**************************
// 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 or PC.
function DetectAll()
{
    if (DetectIphone()){
       alert("IPHONE");
    }else if(DetectIpod()){
       alert("IPOD");
    }else if(DetectPc()){
        alert("Pc");
    }else{
       alert("Other Device");
    }
}
DetectAll();
</script>
</body>
</html>