Saturday, October 13, 2012

How to easily detect and redirect android and iPhone

Simple script for Android detection.

<script type="text/javascript">

         if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "http://www.your_url.com";
}

</script>


Lets specify Android and iPhone version.

<script type="text/javascript">
       var ua = navigator.userAgent;
       if (ua.indexOf("Android") >= 0) {
           var androidversion = parseFloat(ua.slice(ua.indexOf("Android") + 8));
           if (androidversion <= 2.3) {
               window.location = 'http:/www.your_url.com';
           }
       } if (/(iPhone)/i.test(navigator.userAgent)) {
           if (/OS [1-4]_\d(_\d)? like Mac OS X/i.test(navigator.userAgent)) {
               // iOS 1-4 so Do Something            
               window.location = 'http://www.your_url.com';
           }
       }
</script>