{"id":130,"date":"2014-05-19T22:00:47","date_gmt":"2014-05-19T16:00:47","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=130"},"modified":"2014-06-01T09:38:45","modified_gmt":"2014-06-01T03:38:45","slug":"javascript-detection-touch-enabled-devices","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/javascript-detection-touch-enabled-devices\/","title":{"rendered":"Javascript &#8211; Detection of Touch Enabled Devices"},"content":{"rendered":"<p>I needed to determine if a device is touch enabled or not to modify the user experience based on that. \u00a0I know that <a href=\"http:\/\/www.modernizr.com\/\" target=\"_blank\" data-lasso-id=\"84\" rel=\"noopener\">Modernizer<\/a> is the standard for this sort of thing, and I&#8217;ve used it in the past and it does work nicely. \u00a0However I didn&#8217;t want to go that route for two reasons:<\/p>\n<ol>\n<li>In designing a mobile friendly, fast loading site, I didn&#8217;t want to add another library for my users to download when I only needed one feature (that turned out to be one short line of code)<\/li>\n<li>In my research, Modernizer has limitations with IE10. \u00a0Now in fairness, the posts I was reading were about two\u00a0years old and I&#8217;m guessing Modernizer has corrected that issue, but given my first point here I didn&#8217;t want to go that route anyway.<\/li>\n<\/ol>\n<h2>The Solution<\/h2>\n<p>So my solution was this, found via the reference linked below.<\/p>\n<pre lang=\"javascript\">\r\nvar isTouch =(('ontouchstart'in window)||(navigator.msMaxTouchPoints &gt;0));\r\n<\/pre>\n<p>The variable isTouch will return true if you are on a touch screen. \u00a0The<\/p>\n<pre>\r\n'ontouchstart'in window\r\n<\/pre>\n<p>portion covers most all browsers, where as<\/p>\n<pre>navigator.msMaxTouchPoints &gt; 0<\/pre>\n<p>is specifically looking for IE devices, I believe it&#8217;s IE10 and above with the new Windows mobile push that this came about. \u00a0So this should cover all browsers and devices.<\/p>\n<h3>In Use<\/h3>\n<p>With that one line of device feature detection in place, I simply add something like this and we are all set to go.<\/p>\n<pre lang=\"javascript\">\r\nif( isTouch == false ) {\r\n  \/*  Do something that only non-touch devices can benefit from *\/\r\n}\r\n<\/pre>\n<h3>Browsers Tested<\/h3>\n<ul>\n<li>Firefox 29.0.1\u00a0(Windows 8.1)<\/li>\n<li>Chrome 34.0.1847.137 m (Windows 8.1)<\/li>\n<li>IE 11.0.9600.17107\u00a0\u00a0(Windows 8.1)<\/li>\n<li>IE 10.0.9200.16721\u00a0\u00a0(Windows 7)<\/li>\n<li>Safari (iPhone 4s iOS 7.1.1)<\/li>\n<li>Chrome\u00a0(iPhone 4s iOS 7.1.1)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>References<\/h2>\n<ul>\n<li>This is the source of the final solution I found. \u00a0The answer was by\u00a0<em>David<\/em>.<br \/> <a href=\"http:\/\/stackoverflow.com\/questions\/4817029\/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript\" target=\"_blank\" data-lasso-id=\"85\" rel=\"noopener\">http:\/\/stackoverflow.com\/questions\/4817029\/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript<\/a><\/li>\n<li>This article is good to point out that while my solution may be a &#8220;good&#8221; option, it&#8217;s not perfect. \u00a0It&#8217;s a bit of a rant, but a very factual rant so it&#8217;s a good read. \u00a0He does summarize his main point at the end at least ;)<br \/> <a href=\"http:\/\/www.stucox.com\/blog\/you-cant-detect-a-touchscreen\/\" target=\"_blank\" data-lasso-id=\"86\" rel=\"noopener\">http:\/\/www.stucox.com\/blog\/you-cant-detect-a-touchscreen\/<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>I needed to determine if a device is touch enabled or not to modify the user experience based [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12,25,5],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-javascript-jquery","category-responsive-website","category-website-development"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-26","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":5,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}