I am using mod_python
publisher which is set up and working fine. It allows me to run a Python file in my browser and this Python file has HTML embedded in it. I have defined a click-able image by input type = "image" onclick = "some action"
. When I click the image it takes me to a JavaScript function which then performs window.location = "./move"
which then takes me to a Python function. The problem is that the browser window redirects to that function, i. e. localhost/scripts
becomes localhost/scripts/move
. Is it possible to execute the script without having the browser window change? I can post code if needs be.
Asked
Active
Viewed 365 times
0

Armali
- 18,255
- 14
- 57
- 171

Ashill Chiranjan
- 5
- 2
-
Is that your own javascript function which resets window.location? What does the move function do? – sureshvv Sep 30 '15 at 07:36
-
yes it basically justs redirects to the python function. Move allows for a servo motor to move. So i have an arrow button that when i click it causes a servo motor to turn – Ashill Chiranjan Sep 30 '15 at 07:43
-
Can't you redirect back to /scripts at the end of move? – sureshvv Sep 30 '15 at 07:46
-
Yes i can but i have video feed that keeps refreshing when i do that. So basically the whole page refreshes again and the video feed stops and starts – Ashill Chiranjan Sep 30 '15 at 07:50
-
Why are you setting window.location? Can't you just call move()? – sureshvv Sep 30 '15 at 07:50
-
i have tried that but it does not seem to work – Ashill Chiranjan Sep 30 '15 at 08:03
-
You probably want to use some AJAX. – pacholik Sep 30 '15 at 08:07
-
"i have tried that but it does not seem to work" is not helpful. You can use the javascript debugger to see what happens. – sureshvv Sep 30 '15 at 08:20
-
I am sorry about that. Maybe if I post some code would that help? – Ashill Chiranjan Sep 30 '15 at 08:25
-
I have the same need: how to launch python code on the server from javascript without calling a script that will reload page ? (some post without any return value/feedback) – Alexandre Mazel Jan 31 '16 at 17:55
1 Answers
0
In fact, I've just found using Ajax/XmlHttpRequest is simple and work well for that, even if your server is a python SimpleServer style.
You just link this Javascript code from your button:
// send a request, but don't refresh page
xhttp = new XMLHttpRequest();
xhttp.open("GET", "?like", true);
xhttp.send();

Alexandre Mazel
- 2,462
- 20
- 26