admin管理员组

文章数量:1026620

I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.

My current problem is that my Chrome (20.0.01132.47 m) returns true for ('ontouchstart' in window) but it does not fire the touch events.

When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.

Can I tell from script if "Emulate touch events" is enabled or not?

JQuery based answers are fine.

I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.

My current problem is that my Chrome (20.0.01132.47 m) returns true for ('ontouchstart' in window) but it does not fire the touch events.

When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.

Can I tell from script if "Emulate touch events" is enabled or not?

JQuery based answers are fine.

Share Improve this question edited Jun 22, 2020 at 14:39 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 16, 2012 at 13:55 epelegepeleg 11k18 gold badges111 silver badges158 bronze badges 1
  • Here's a list of tests and false positives modernizr.github./Modernizr/touch.html – Prinzhorn Commented Oct 30, 2012 at 15:52
Add a ment  | 

2 Answers 2

Reset to default 1

Can I tell from script if "Emulate touch events" is enabled or not ?

Try to check for more than one finger ;-)

Perhaps this helps?

window.addEventListener('touchstart', function(event) {
    var emulate = event.targetTouches.length == 2;
    alert(emulate ? true : false);
}, false);

BTW: 'ontouchstart' in window results in false (chrome v21.0.1180.60) unless you have closed the developer toolbar.

Fiddle

Best way by js:

function is_touch_device() {
    return 'ontouchstart' in window || navigator.maxTouchPoints;
}
console.log(is_touch_device())

I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.

My current problem is that my Chrome (20.0.01132.47 m) returns true for ('ontouchstart' in window) but it does not fire the touch events.

When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.

Can I tell from script if "Emulate touch events" is enabled or not?

JQuery based answers are fine.

I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.

My current problem is that my Chrome (20.0.01132.47 m) returns true for ('ontouchstart' in window) but it does not fire the touch events.

When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.

Can I tell from script if "Emulate touch events" is enabled or not?

JQuery based answers are fine.

Share Improve this question edited Jun 22, 2020 at 14:39 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 16, 2012 at 13:55 epelegepeleg 11k18 gold badges111 silver badges158 bronze badges 1
  • Here's a list of tests and false positives modernizr.github./Modernizr/touch.html – Prinzhorn Commented Oct 30, 2012 at 15:52
Add a ment  | 

2 Answers 2

Reset to default 1

Can I tell from script if "Emulate touch events" is enabled or not ?

Try to check for more than one finger ;-)

Perhaps this helps?

window.addEventListener('touchstart', function(event) {
    var emulate = event.targetTouches.length == 2;
    alert(emulate ? true : false);
}, false);

BTW: 'ontouchstart' in window results in false (chrome v21.0.1180.60) unless you have closed the developer toolbar.

Fiddle

Best way by js:

function is_touch_device() {
    return 'ontouchstart' in window || navigator.maxTouchPoints;
}
console.log(is_touch_device())

本文标签: javascript(39ontouchstart39 in window) returns true but no touch eventsStack Overflow