Prevent browser from reloading main page after mouse event

I have a web page that allows me to send serial commands to serial port on my raspberry pi. Main page is in html and its have images in it, and when i click on image javascript calls cgi file that sends command to serial. Everything is works fine on desktop browser, when i press on image im staying on the same page, but when i do it on iphone or ipad its redirecting me to location of cgi file. Here is my code 1. HTML

<img src="img/sound.png" alt="sound" id="sound_b" ontouchstart="changeImageSound(), sound()" ontouchend="changeImageSound()">

2.JavaScript

function sound() {
document.location="helpers/sound.cgi";
}

3.cgi

#!/usr/bin/python
print "Status: 204 No Content"
print "Content-type: text/plain"
print ""
import cgi
import serial
import time

# Open a serial connection to Roomba
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)

# Assuming the robot is awake, start safe mode so we can hack.
ser.write('\x83')
time.sleep(.1)

# Program a five-note start song into Roomba.
ser.write('\x8c\x00\x05C\x10H\x18J\x08L\x10O\x20')

# Play the song we just programmed.
ser.write('\x8d\x00')
time.sleep(1.6) # wait for the song to complete

# Leave the Roomba in passive mode; this allows it to keep
#  running Roomba behaviors while we wait for more commands.
ser.write('\x80')

# Close the serial port; we're done for now.
ser.close()

Answers


You just add this to your Javascript code,

event.preventDefault();

you can add the javascript code mentioned below to the sound function but it would require the event object inside it. else u can have it in the ontouchstart directly

event.preventDefault();

Need Your Help

Debugging native code when called from managed

c# c++ .net-4.0

I've written a C# application, where much of the work is done in a C++ back-end. Now I've been actively developing both sides, and the current iteration of the back end throws an access violation i...

stack level deep too error

ruby-on-rails ruby

I have installed Ruby 1.9.2 with RVM on my system. When I am trying to start the rails server I am getting the following error: