mobile - Posts

\n\n\n\nNow, when you register an event listener in your app using $(…).click(), the respective element should be pleasant to use for both desktop and mobile users.\n\n## References\n\n[The Current State of (Touch) Events](http://blogs.adobe.com/adobeandjquery/2011/03/07/the-current-state-of-touch-events/)\n\n[Tap vs. Click: Death by Ignorance](https://coderwall.com/p/bdxjzg)\n","contentHtml":"

Problem

\n

One of the most common things I do with JQuery is to attach click event listeners to buttons, links and other elements as follows:

\n

$(".class").click(function(evt) {});

\n

Unfortunately, I found that, while this approach works well for Desktop browsers, the user interaction on touch-enabled devices feels very awkward and sluggish.

\n

Solution

\n

Since this problem is of relevance for every mobile web application, numerous solutions have been proposed. All mobile UI JavaScript frameworks I know of provide built in support for 'tab' events, as opposed to 'click' events.

\n

However, often we would just like to support being able to interact with simple buttons on our page regardless of whether a user accesses the site through a desktop or mobile browser; ideally, without adding another more or less 'heavy' framework to our site.

\n

Luckily, jQuery Mobile is built in a very modular way and we can extract the particular component of the framework to support good support for tabs and clicks. Head to http://jquerymobile.com/download-builder/ and only check the 'Touch' component under Events.

\n

\"\"

\n

Then, let the jQuery Mobile site build your download and add it to your page. For a quick test, you can also use the script provided below.

\n

Next, we can rewire all calls to $(…).click() using the following snippet:

\n<script src=\"http://u1.linnk.it/qc8sbw/usr/apps/textsync/upload/jquery-mobile-touch.value.js \" ><script>\n\n<script>\n\n$.fn.click = **function**(listener) {\n\n    **return** **this**.each(**function**() {\n\n       **var** $this = $( **this** );\n\n       $this.on('vclick', listener);\n\n    });\n\n};\n\n<script>\n\n

Now, when you register an event listener in your app using $(…).click(), the respective element should be pleasant to use for both desktop and mobile users.

\n

References

\n

The Current State of (Touch) Events

\n

Tap vs. Click: Death by Ignorance

\n","sk":"2013/09/04/touch-and-click-in-jquery-without-jquery-mobile","pk":"maxrohde.com#Post","tags":"jquery,mobile","blog":"maxrohde.com","title":"Touch and Click in jQuery (without jQuery Mobile)","authors":"max","path":"2013/09/04/touch-and-click-in-jquery-without-jquery-mobile"},{"summary":"JQuery and/or JavaScript have no native mechanism to determine whether a page is loaded on a mobile phone or not. However, CSS media queries offer thi...","_et":"Post","_ct":"2024-04-06T06:55:07.189Z","datePublished":"2012-08-29T00:00:00.000Z","_md":"2024-04-06T06:55:07.189Z","categories":"javascript","contentMarkdown":"\nJQuery and/or JavaScript have no native mechanism to determine whether a page is loaded on a mobile phone or not. However, CSS media queries offer this capability. I found the following [nice trick on stackoverflow](http://stackoverflow.com/a/10364620/270662) to utilize media queries to help JS/JQuery applications to determine whether they run in a mobile.\n\nFirst, insert an arbitrary div somewhere in the body of your HTML document such as:\n\n
\n\nThen, add the following CSS:\n\n```css\n@media only screen and (max-width: 480px) {\n #mobileDetector {\n display: none;\n }\n}\n```\n\n(Note: You can also use the width 767px or so to also capture tablets).\n\nFinally, the following JQuery function can be used to determine if on a mobile or not:\n\n```javascript\n(function ($) {\n $.isMobile = function () {\n return $('#mobileDetector').css('display') === 'none';\n };\n})(jQuery);\n```\n","contentHtml":"

JQuery and/or JavaScript have no native mechanism to determine whether a page is loaded on a mobile phone or not. However, CSS media queries offer this capability. I found the following nice trick on stackoverflow to utilize media queries to help JS/JQuery applications to determine whether they run in a mobile.

\n

First, insert an arbitrary div somewhere in the body of your HTML document such as:

\n
\n\n

Then, add the following CSS:

\n
@media only screen and (max-width: 480px) {\n  #mobileDetector {\n    display: none;\n  }\n}\n
\n

(Note: You can also use the width 767px or so to also capture tablets).

\n

Finally, the following JQuery function can be used to determine if on a mobile or not:

\n
(function ($) {\n  $.isMobile = function () {\n    return $('#mobileDetector').css('display') === 'none';\n  };\n})(jQuery);\n
\n","sk":"2012/08/29/using-jquery-to-detect-if-on-a-mobile-phone","pk":"maxrohde.com#Post","tags":"jquery,mobile","blog":"maxrohde.com","title":"Using JQuery to Detect If on a Mobile Phone","authors":"max","path":"2012/08/29/using-jquery-to-detect-if-on-a-mobile-phone"}]};window.GOLDSTACK_DEPLOYMENT="prod"