I have a variable I want to pass to a cgi script. Then have the cgi script process the variable and return it for use in a javascript. Can this be done?
Sure,
To get it to the CGI you could add it to the URL:
location.href='script.cgi?Parameter='+myvar;
or by putting it in a hidden form field and submitting the form:
<script>
document.f.Parameter.value=myval;
</script>
<form name="f" action="script.cgi" method="post">
<input type="hidden" name="Parameter">
...
<input type="submit">
</form>
To get the variable back into JavaScript, have the CGI write the JavaScript. I use ASP, so I would use:
Response.Write "<script>var myvar=" & myvar & "';</script>"
In PHP it might be something like:
printf("<script>var myvar='%s'</script>",$myvar);
I just reread your question and thought of an additional comment. If you wanted to do it without leaving/reloading the page, you could send the parameter (using either of the techniques I described above) to a hidden frame and have the script write the output to the hidden frame like:
Response.Write "<script>parent.visibleFrame.myvar='" & myvar & "';</script>"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.