30

I use a media query to load in a stylesheet (if device is a tablet).

I was just wondering how I could check in my javascript if the user is using a tablet, is there a check or should I just check what stylesheet was loaded in?

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
panthro
  • 19,109
  • 50
  • 152
  • 271
  • The media query is most likely detecting tablet based on screen size. You could do the same thing with JS by detecting the Window size. – robmisio Apr 24 '12 at 15:23
  • I don't agree that this is a duplicate. A tablet is a mobile device, but a mobile device is not always a tablet. – Joeri Jul 17 '15 at 07:07

3 Answers3

13

Try to see if perhaps this sheds a light on your question:

What is the best way to detect a mobile device in jQuery?

As the answer to that thread says, I'd suggest to rely on 'feature detection' rather than tablet vs non-tablet (as it may not be as straightforward as one may think) hence --> http://modernizr.com/

Community
  • 1
  • 1
Alucardz
  • 508
  • 4
  • 11
  • I agree with using feature detection whenever possible, but there are some scenarios when using userAgents may work better. What if I wanted to have crazy canvas image filters that bogged down the processor to not run on tablets/mobile devices? – Aaron Oct 30 '12 at 18:39
1

Try passing the useragent from the server into the page so that JavaScript knows exactly what browser is being reported.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Surreal Dreams
  • 24,368
  • 3
  • 42
  • 57
1

You can use navigator.userAgent and do a substring search to find out which tablet viewing your site. Here is a current list of tablet userAgent http://mojosunite.com/tablet-user-agent-strings

Ballbin
  • 19
  • 2