I'm trying to write a web application that can respond to both touch events and mouse events. Webview on android (and maybe other environments) trigger a mousedown event and a touchstart event when the phone is tapped. I'm running into a problem where the touchstart event triggers a page change, and the mousedown event is clicking a different button in the new page.
If its possible, the easy solution would be to force webview to not trigger mouse events alongside touch events (but only when an actual mouse is clicked).
I've read the answer here: How to bind 'touchstart' and 'click' events but not respond to both? but it doesn't cover this case.
Anyone know how I can solve this problem in a general way?
Related
I've used onclick events in my website. But when I open it in google chromes' developer mode's mobile view, nothing happens on touch on the elements which work on click with mouse. So my question is:
Do I have to also add ontouch events along with onclick events, or onClick event work on touch on all touch-screen devices?
P.S: You can see all of my codes here: https://github.com/SycoScientistRecords/sycoscientistrecords.github.io/
Or at the live website: http://sycoscientistrecords.github.io
And no I haven't tested the website on real phone.
onclick works fine on touchscreens; I've used it several times and have never had any problem.
You could consider using onmousedown instead of onclick. Or use jQuery to detect taps.
I found this detailed writeup at MDN very helpful. In particular:
the browser may fire both touch events and mouse events in response to the same user input [emphasis mine]
and
the element's touch event handlers should call preventDefault() and no additional mouse events will be dispatched
So, your touchstart or touchend listener can call evt.preventDefault() and your mousedown / mouseup listeners won't fire because they come later in the chain.
In Angular, I was able to detect whether I'd clicked a button using my mouse or my laptop's touchscreen, by changing (click)="doSomething()" to (mouseup)="doSomething(false)" (touchend)="doSomething(true); $event.preventDefault()". The method is called with true for touch events and false for mouse events.
onclick may not work on touch devices, I had this issue and the event ontouchstart sorts it.
if you use ontouchstart and onclick watch that you don't trigger the event twice.
this is another post related
onClick not working on mobile (touch)
New browsers have a pointerType which determines if the onClick is made by a mouse or via a touch. If you just want make adjustments in user behavior based on the input, using pointerType is the safest way.
if you are using jQuery:
$(selector).click(e => {
if (e.pointerType === "mouse") {} // mouse event
else {} // touch event
});
if you are using vanilla JS:
element.addEventListener('click', e => {
if (e.pointerType === "mouse") {} // mouse event
else {} // touch event
});
If you are using React, the event is wrapped around a synthetic event. To access the pointerType, you have to use the nativeEvent of the react event. Here is what you need to consider (especially if you are using Typescript). If the event is triggered by a mouse, the native event is an instance of MouseEvent which does not have pointerType, so, first you need to check the type of native event which will also take care of the typing problems in TS
<div
onClick={e => {
if (e.nativeEvent instanceof PointerEvent && e.nativeEvent.pointerType === 'touch') {} // Touch Event
else {} // Mouse Event
}}
></div>
Pro tip: If you want to test the touch event in development, use Chrome following this. Note that Safari has a responsive mode which simulates the framework of iPhones and iPads. However, Safari always registers a mouse event even when you are in responsive design mode and have selected an iPhone or iPad.
I am curious about this one. Does creating a div imitating a button (with 'click' event binded to it) is user-friendly? I mean, does all mobile browsers accurately treat it and always fire event when div is clicked?
Does replacing such constructions with normal buttons increases responsivness on mobile devices?
Google describes this pretty well i think here.
So as described there, handling the click event adds a 300ms delay because it is waiting to see if it is a double-tap.
The technique involves a bit of JavaScript that allows the button to
respond to touchend events rather than click events. Touchend events
are fired with no delay so this is significantly faster than click
events, however there are a few problems to consider:
If the user tapped somewhere else on the screen and then invokes a
touchend on the button then we should not fire a click.
If the user touches down on the button and then drags the screen a bit and then
invokes a touchend on the button then we should not fire a click.
We want to highlight the button when the user touches down to give it a
pressed state.
We can solve the first two problems by monitoring touchstart and touchmove events as well.
We should only consider a touchend on the button if there was previously a touchstart on the button. Also if there exists a touchmove anywhere that goes past a
certain threshold from the touchstart then we should not handle the
touchend as a click.
We can solve the third problem by adding an onclick handler to the
button as well. Doing so will allow the browser to properly treat it
as a button, and our touchend handler will ensure that the button is
still fast. Also, the presence of an onclick handler serves as a good
fallback for browsers that don’t support touch events.
Another advice from experience would be to avoid anchors for buttons.
They recommend using Touchend instead of click.
I have a website I have written which is cross platform. On the iPad I have to use the mouseup and mouseup events to simulate a click (because it is events imbedded within SVG inside an iFrame and there are special events if someone presses and holds, or presses and drags or presses and releases (clicks))
Here is what is happening.
touchStart event triggered.
touchEnd event triggered.
My script sees that this is a click event and then triggers the following event:
window.parent.window.location.href = "../TRL/SelectHotspot.aspx" + returnString
The browser then goes to that page, but then (strange this...) the control that is in that exact position on the target page recieves a click event. This means that it then moves onto the next page.
Does anyone know what would cause this and how to stop it please? It doesn't happen on desktops (they don't expose the touchstart / touchend event anyway) and not on any android devices.
I've scoured the internet for this answer and been unsuccessful.
Mobile devices are by default slower using the 'click' event because it continues to listen for other types of gestures, such as the double click, scroll, click and hold, etc. Hence, jquery mobile is slow and unresponsive when trying to expand a collapsible list using 'Click'. It delays about .5 second before triggering.
What is the easiest way that I can unbind 'Click' events from these elements and bind the 'tap' event to perform the SAME action?
My plan is to detect if the device is mobile, and if yes, then add the Tap event and thus making the site more responsive.
THANKS!
I have various touchEvents detection on a page on Safari Mobile (a controller, if you want to know...). They all work well indepedantly, but whenever I have a first touchpoint pressed, If I touch a new point, the events are triggered when I trigger a new one.
For example :
I hold the stick with a finger. It's not moving
I press a button, a touchStart event should be triggered but is not
If I move the stick, the touchStart event is triggered
If I let go of the button, the touchStart is triggered, but not the touchEnd
If I move the stick (touchMove), the touchEnd is triggered
You understand how in my case it's a problem. I get buttons triggered with delay, or getting stuck until I move the stick, etc...
Is there a workaround ? thanks