passing Python data to a javascript function
Bill Allen
wallenpb at gmail.com
Wed Oct 26 17:32:24 EDT 2011
I am writing a Python CGI and am needing to pass a data value from Python to
a javascript function. My understanding is that I should use JSON as the
middleman. However, I have not found a good example of doing this. The
piece of data is a simple integer, but I converting that to a string first.
Here is what I am trying, but unsuccessfully. I know this code is broken
in at least two places. I may be going about this completely the wrong
way. Any help would be appreciated.
Thanks,
Bill Allen
#!/usr/bin/python
import json
import os
import cgi
import cgitb
cgitb.enable()
pid = [{'p':str(os.getpid())}]
pid_data = json.dumps(pid)
print "Content-type: text/html"
print
print """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>showPID</title>
<script type="text/javascript">
function showPID(pid_data)
{
var a_pid=eval("(" + pid_data + ")"); ####This is where I am not sure
what do with the value once I get it into the function.
document.getElementById('txt').innerHTML=a_pid;
}
</script>
</head>
<html>
<body onload="showPID()"> ####This is where I am not sure how to pass the
value to the javascript function
<div id="txt"></div>
Hello World!<br>
</body>
</html>"""
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111026/e14f4dcf/attachment.html>
More information about the Python-list
mailing list