<br clear="all">Benjamin,<br><br>I was afraid I was doing that. I have simplified it quite a bit, still not getting the output I am looking for. I am down to that I am not passing the value in the onload=showPID() call correctly. I know this is getting a bit far from a Python issue now, but if you have more insight on this, I would appreciate it. Is there another way of passing the data from the Python script to the javascript than what I am doing in this CGI?<br>
<br>Thanks,<br>Bill<br><br><br>code now is:<br><br>import os, cgi, cgitb<br>cgitb.enable()<br><br>pid_data = str(os.getpid())<br>print "Content-type: text/html"<br>print<br><br>print """<br><html><br>
<head><title>Test Page</title><br><script type="text/javascript"><br>function showPID(pid){<br>document.getElementById('txt').innerHTML="js: "+pid;<br>}<br></script><br>
</head><br><body onload="showPID(pid_data)"><br><br>Hello World!<br><br><br><div id="txt"></div><br><br>"""<br>print "html: "+pid_data+"<br>"<br>
print """<br></body><br></html>"""<br><br><br>If I use <body onload="showPID(pid_data)"> I get this for output:<br>Hello World!<br><br>
<br>
html: 4900<br><br><br>
If I use <body onload="showPID('pid_data')"> I get this for output:<br>Hello World!<br><br>
<div id="txt">js: pid_data</div><br>
html: 4708<br><br><br><br>
<br><br><div class="gmail_quote">On Wed, Oct 26, 2011 at 17:58, Benjamin Kaplan <span dir="ltr"><<a href="mailto:benjamin.kaplan@case.edu">benjamin.kaplan@case.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Wed, Oct 26, 2011 at 6:18 PM, Bill Allen <<a href="mailto:wallenpb@gmail.com">wallenpb@gmail.com</a>> wrote:<br>
><br>
> 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 could easily covert that to a string first if necessary. Here is what I am trying, but unsuccessfully. I am sure that I have more than one issue in this code.<br>
><br>
><br>
><br>
> #!/usr/bin/python<br>
><br>
> import json, os, cgi, cgitb<br>
> cgitb.enable()<br>
><br>
> pid = [{'p':str(os.getpid())}]<br>
> pid_data = json.dumps(pid)<br>
><br>
> print "Content-type: text/html"<br>
> print<br>
><br>
> print """<br>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"<br>
> "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</a>"><br>
> <html xmlns="<a href="http://www.w3.org/1999/xhtml" target="_blank">http://www.w3.org/1999/xhtml</a>"><br>
> <head><title>Test Page</title><br>
> <script type="text/javascript"><br>
> function showPId(pid_data)<br>
> {<br>
> var a_pid=eval("(" + pid_data + ")");<br>
> document.getElementById('txt').innerHTML=a_pid;<br>
> }<br>
> </script><br>
> </head><br>
> <body onload="startTime({0})"><br>
><br>
> <center><div id="txt"></div></center><br><br>
><br>
> </body><br>
> </html>""".format(pid_data)<br>
><br>
<br>
</div></div>You're making this much more difficult than it needs to be. JSON is<br>
used for sending data to JavaScript, meaning the JavaScript asks the<br>
server for a bunch of data, the server sends the client JSON, and<br>
everyone is happy. In your case, the information is available when the<br>
javascript is being generated so you can just pass in the number, no<br>
JSON needed.<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>