Last Updated: February 25, 2016
·
1.448K
· francisc

Check for touch support

You can use this to check if the current User Agent supports touch.
For a more thorough check, look at the Modernizr implementation.

var hasTouch=!!('ontouchstart' in window);

2 Responses
Add your response

  1. expression 'ontouchstart' in window will return boolean value and it's not required to create boolean manualy (using !!)

  2. expression window.hasOwnProperty('ontouchstart') is faster :)

over 1 year ago ·

And window['prop'] even faster, but those two yield false.
The double negation is for clarity that it's a boolean.
Thanks for the comment.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside