5

I know a lot of JavaScript libraries are depending on "ontouchstart" to detect if it's on tablet or a desktop.

Here's an example of code I'm talking about:

    var hasTouch = ("ontouchstart" in window);

For now, I had to comment out all the tablet detection code for it to work.

What would be the best way of detecting a tablet vs a desktop?

Thanks!

Simon Germain
  • 6,834
  • 1
  • 27
  • 42

1 Answers1

3

To see if its a mobile platform I've used this

var iPadAgent = navigator.userAgent.match(/iPad/i) != null;
var iPodAgent = navigator.userAgent.match(/iPhone/i) != null;
var AndroidAgent = navigator.userAgent.match(/Android/i) != null;
var webOSAgent = navigator.userAgent.match(/webOS/i) != null;

var isMobile = iPadAgent || iPodAgent || AndroidAgent || webOSAgent;

It works pretty well.

fcortes
  • 112
  • 2
  • 8