Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if touch device with ie11 using javascript

I'm trying to use javascript to detect if my visitor is on a touch device or not.

I have the following...

function is_touch_device() {
    return 'ontouchstart' in window || 'onmsgesturechange' in window; 
}

The above works fine apart from ie11 is returning true, that it is a touch device when in reality it isnt. Has anybody experienced this before?

like image 868
Liam Avatar asked Feb 13 '23 18:02

Liam


1 Answers

The following code snippet might help:

function is_touch_device() {
 return (('ontouchstart' in window)
      || (navigator.maxTouchPoints > 0)
      || (navigator.msMaxTouchPoints > 0));
 //navigator.msMaxTouchPoints for microsoft IE backwards compatibility
}
like image 62
Jack_of_All_Trades Avatar answered Feb 15 '23 11:02

Jack_of_All_Trades