Javascript detect touch device Snippet

Probably the most reliable way to detect a touch device would be using the Modernizr jQuery plugin, but in some cases you might not want to introduce another plugin just to be able to do that. In this case you can use the following Javascript snippet to detect if your website is been loaded on a mobile or any other other touch device.

Javascript to detect touch device.

function is_touch_device() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
};

Please let me know if this article helped you and share your thoughts in the comment section. If the code snippet helped you a lot, you can buy me a beer any time using the Beer button in the sidebar 🙂 Cheers!

Lehel holds a BS in Computer science and works as Front End Engineer. He enjoys discovering life hacks and hacks in general. Among many other things he is interested in topics such as Consciousness, Philosophy, Economy and Game theory.