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

I am trying to write code that will run either in a browser or as a phonegap application.One of the th

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())

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648665a4638124.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信