How to detect a custom touch screen using JS

I use phonegap to create android apps.

I would like to detect a touch event from the user so that I can pop up an alert. However, how can I name the ontouch event from javascript?

Thanks!

+6
javascript android cordova touch
source share
1 answer

Below is an example showing touchstart and touchend . It demonstrates two different ways to connect touch events: element attributes or JavaScript addEventListener .

Since it listens for touch events, events will not fire in the desktop browser (which supports mouse events). To check the page, you can open its simulator Android or iOS.

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0;" /> <style type="text/css"> a { color:black; display:block; margin:10px 0px; } </style> <script type="text/javascript"> function onload() { document.getElementById('touchstart').addEventListener('touchstart', hello, false); document.getElementById('touchend').addEventListener('touchend', bye, false); } function hello() { alert('hello'); } function bye() { alert('bye'); } </script> <title>Touch Example</title> </head> <body onload="onload();"> <h1>Touch</h1> <a href="#" ontouchstart="hello();return false;">Attribute: ontouchstart</a> <a href="#" ontouchend="bye();return false;">Attribute: ontouchend</a> <a href="#" id="touchstart">addEventListener: touchstart</a> <a href="#" id="touchend">addEventListener: touchend</a> </body> </html> 
+15
source share

All Articles