3

I have this line of code which detects all touch devices:

<script>if( 'ontouchstart' in window ) window.location = 'mobile.html';</script>

I just want modify it so that it ONLY targets touch enabled mobile devices but also excludes tablet devices. How do I do this without being too specific?

Kara
  • 5,650
  • 15
  • 48
  • 55
egr103
  • 3,334
  • 14
  • 60
  • 105
  • this might help http://stackoverflow.com/questions/3514784/best-way-to-detect-handheld-device-in-jquery – Pete Feb 22 '13 at 15:19

1 Answers1

4

If you are wanting to redirect a user to your mobile site you would use this:

if (screen.width <= 768) {
        window.location = "mobile.html";
    }

If you want to find out if its touch screen and less than 768px I think you could use this:

var is_touch_device = 'ontouchstart' in document.documentElement;

var viewport = $(window).width()

if (is_touch_device == true && viewport < 768) {

   (Code goes here)

}
Alex
  • 2,215
  • 1
  • 22
  • 41