Detecting touch screen devices with Javascript

The question:

In Javascript/jQuery, how can I detect if the client device has a mouse?

I’ve got a site that slides up a little info panel when the user hovers their mouse over an item. I’m using jQuery.hoverIntent to detect the hover, but this obviously doesn’t work on touchscreen devices like iPhone/iPad/Android. So on those devices I’d like to revert to tap to show the info panel.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

var isTouchDevice = 'ontouchstart' in document.documentElement;

Note: Just because a device supports touch events doesn’t necessarily mean that it is exclusively a touch screen device. Many devices (such as my Asus Zenbook) support both click and touch events, even when they doen’t have any actual touch input mechanisms. When designing for touch support, always include click event support and never assume any device is exclusively one or the other.

Method 2

Found testing for window.Touch didn’t work on android but this does:

function is_touch_device() {
  return !!('ontouchstart' in window);

See article: What’s the best way to detect a ‘touch screen’ device using JavaScript?

Method 3

+1 for doing hover and click both. One other way could be using CSS media queries and using some styles only for smaller screens / mobile devices, which are the ones most likely to have touch / tap functionality. So if you have some specific styles via CSS, and from jQuery you check those elements for the mobile device style properties you could hook into them to write you mobile specific code.

See here:

Method 4

if ("ontouchstart" in window || navigator.msMaxTouchPoints) {
    isTouch = true;
} else {
    isTouch = false;

Works every where !!

Method 5

return (('ontouchstart' in window)
      || (navigator.maxTouchPoints > 0)
      || (navigator.msMaxTouchPoints > 0));

Reason for using maxTouchPoints alongwith msMaxTouchPoints:

Microsoft has stated that starting with Internet Explorer 11,
Microsoft vendor prefixed version of this property (msMaxTouchPoints)
may be removed and recommends using maxTouchPoints instead.

Source :

Method 6

I use:

    alert('Touch enabled');

in jQuery mobile 1.0.1

Method 7

Google Chrome seems to return false positives on this one:

var isTouch = 'ontouchstart' in document.documentElement;

I suppose it has something to do with its ability to “emulate touch events” (F12 -> settings at lower right corner -> “overrides” tab -> last checkbox). I know it’s turned off by default but that’s what I connect the change in results with (the “in” method used to work in Chrome).
However, this seems to be working, as far as I have tested:

var isTouch = !!("undefined" != typeof document.documentElement.ontouchstart);

All browsers I’ve run that code on state the typeof is “object” but I feel more certain knowing that it’s whatever but undefined 🙂

Tested on IE7, IE8, IE9, IE10, Chrome 23.0.1271.64, Chrome for iPad 21.0.1180.80 and iPad Safari. It would be cool if someone made some more tests and shared the results.

Method 8

Wrote this for one of my sites and probably is the most foolproof solution. Especially since even Modernizr can get false positives on touch detection.

If you’re using jQuery

  mouseover : function(){
    Modernizr.touch = false; // Add this line if you have Modernizr

or just pure JS…

window.onmouseover = function(){ 
    window.onmouseover = null;
    document.getElementsByTagName("html")[0].className += " mouse";

Method 9

For my first post/comment:
We all know that ‘touchstart’ is triggered before click.
We also know that when user open your page he or she will:
1) move the mouse
2) click
3) touch the screen (for scrolling, or … 🙂 )

Let’s try something :

//–> Start: jQuery

var hasTouchCapabilities = 'ontouchstart' in window && (navigator.maxTouchPoints || navigator.msMaxTouchPoints);
var isTouchDevice = hasTouchCapabilities ? 'maybe':'nope';

//attach a once called event handler to window

$(window).one('touchstart mousemove click',function(e){

    if ( isTouchDevice === 'maybe' && e.type === 'touchstart' )
        isTouchDevice = 'yes';

//<– End: jQuery

Have a nice day!

Method 10

I have tested following code mentioned above in the discussion

 function is_touch_device() {
    return !!('ontouchstart' in window);

works on android Mozilla, chrome, Opera, android default browser and safari on iphone…
all positive …

seems solid for me 🙂

Method 11

A helpful blog post on the subject, linked to from within the Modernizr source for detecting touch events. Conclusion: it’s not possible to reliably detect touchscreen devices from Javascript.

Method 12

This works for me:

function isTouchDevice(){
    return true == ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);

Method 13

If you use Modernizr, it is very easy to use Modernizr.touch as mentioned earlier.

However, I prefer using a combination of Modernizr.touch and user agent testing, just to be safe.

var deviceAgent = navigator.userAgent.toLowerCase();

var isTouchDevice = Modernizr.touch || 
(deviceAgent.match(/(iphone|ipod|ipad)/) ||
deviceAgent.match(/(android)/)  || 
deviceAgent.match(/(iemobile)/) || 
deviceAgent.match(/iphone/i) || 
deviceAgent.match(/ipad/i) || 
deviceAgent.match(/ipod/i) || 
deviceAgent.match(/blackberry/i) || 

if (isTouchDevice) {
        //Do something touchy
    } else {
        //Can't touch this

If you don’t use Modernizr, you can simply replace the Modernizr.touch function above with ('ontouchstart' in document.documentElement)

Also note that testing the user agent iemobile will give you broader range of detected Microsoft mobile devices than Windows Phone.

Also see this SO question

Method 14

In jQuery Mobile you can simply do:


Don’t know why this is so undocumented.. but it is crossbrowser safe (latest 2 versions of current browsers).

Method 15

As already mentioned, a device may support both mouse and touch input. Very often, the question is not “what is supported” but “what is currently used”.

For this case, you can simply register mouse events (including the hover listener) and touch events alike.




JavaScript should automatically call the correct listener based on user input. So, in case of a touch event, onTouchStartCallback will be fired, emulating your hover code.

Note that a touch may fire both kinds of listeners, touch and mouse. However, the touch listener goes first and can prevent subsequent mouse listeners from firing by calling event.preventDefault().

function onTouchStartCallback(ev) {
    // Call preventDefault() to prevent any further handling
    your code...

Further reading here.

Method 16

For iPad development I am using:

  if (window.Touch)
    alert("touchy touchy");
    alert("no touchy touchy");

I can then selectively bind to the touch based events (eg ontouchstart) or mouse based events (eg onmousedown). I haven’t yet tested on android.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment