opensubscriber
   Find in this group all groups
 
Unknown more information…

p : python-list@python.org 27 October 2011 • 5:32AM -0400

passing Python data to a javascript function
by Bill Allen

REPLY TO AUTHOR
 
REPLY TO GROUP




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>"""

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

opensubscriber is not affiliated with the authors of this message nor responsible for its content.