Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent "ghost" clicks on hyperlinks when tapping on touch device

When I touch exactly on the border of an a tag element, it triggers a click event, but not a touchstart event. Why is this happening?

To show the problem I made this fiddle: http://jsfiddle.net/o8cvqL0L/8/ the a tag has 2 events:

$('#yellow').on('touchstart', function(e) {
    alert('touch');
    e.preventDefault();
});

$('#yellow').on('click', function(e) {
    alert('click');
});

Just make a touch exactly on the border of the yellow element. Is there any way to avoid this behaviour?

I tested this on Android Browser, Safari and Desktop Chrome.

Thank you very much!

EDITED: The touch must be outside of the element but still within the radiusX/Y of the Touch to become a click. The values of clientX/Y and pageX/Y in the click listener are not right, showing allways coordinates corresponding to the element when it´s not so. (http://jsfiddle.net/o8cvqL0L/35)

Having a container with touchstart listener seems making that these weird behaviour in the child element disappear, and the coordinates in the event object are right. (http://jsfiddle.net/o8cvqL0L/42/) Then, using event delegation fixes the issue, BUT only when the target element is a div and not a link like in my previous example. Here the solution for div elements: http://jsfiddle.net/o8cvqL0L/44/

I´m still looking for a solution for a tags and a documented reason for these issue.

like image 485
Sadako Avatar asked Dec 01 '14 09:12

Sadako


1 Answers

The touchstart event is actually fired in the code you provided.

In this fiddle http://jsfiddle.net/mssavai/o8cvqL0L/34/ I have modified your original code to display a border around #yellow. You'll see that touching the border area generates a touchstart.

However, touching near the edge will sometimes fire a click, which I think is the issue you are facing. From my observation (on Google chrome - android), this occurs if the touch begins outside the registered area, and then grows to cover part of that area when more pressure is exerted as part of the touch.

like image 165
Simon Savai Avatar answered Nov 15 '22 12:11

Simon Savai