2

I know that most people try to avoid Javascript mobile detection because not all mobile browsers support it, but I am using it in a Javascript function so if the browser doesn't support Javascript, it doesn't matter if it's mobile since it won't be able to use the function. I don't want it to redirect; just to return whether the browser is mobile or not. Any ideas?

Zak
  • 2,688
  • 4
  • 29
  • 45
  • 2
    What do you mean by, "return"? What is it in particular about a device's "mobileness" that's interesting to you? – Pointy Dec 23 '10 at 16:10
  • I mean I want something along the lines of function isMobile() that returns true if it is mobile or false if it is not. As for the "mobileness", I am specifically trying to find touch screen mobile devices. The function I am working on is a "tooltip" function, and I'd either want to change the displaying of the tooltip to only on the click of the question mark icon or even disable it all together and not even display the icon. – Zak Dec 23 '10 at 16:16
  • Most mobile browsers support JavaScript nowadays. You probably not want to detect mobile browsers, but JavaScript-less browsers. The best solution would be build your site to work with any JavaScript at all, and then add JavaScript for additional features on top. – RoToRa Dec 23 '10 at 16:21

3 Answers3

3
var isTouchDevice =  ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;

in modernizr ,it is like this.

ZHAO Xudong
  • 1,371
  • 1
  • 12
  • 21
1

See: Standard way to detect mobile browsers in a web application based on the http request

Community
  • 1
  • 1
beeglebug
  • 3,522
  • 1
  • 23
  • 39
1

You can use handsetdetection.com to detect mobile browsers in javascript. We do a detection based on the http headers and send javascript to the client. (disclaimer, i work there).

Piotr Salaciak
  • 1,653
  • 1
  • 15
  • 28
Richard
  • 279
  • 3
  • 4