最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ('ontouchstart' in window) returns true but no touch events - Stack Overflow

matteradmin6PV0评论

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())
Post a comment

comment list (0)

  1. No comments so far