View Full Version : Assign a variable (JSP) from within a script

10-31-2006, 12:02 AM
I know the subject heading is not so clear but I will do my best to explain.

Basically what I am doing is reading some values from a javascript that reads from an XML file. What I am trying to accomplish is a way to assign a variable that was declared within the "<% .. String strValue="" .... %>" section of my JSP page with a value returned from the script. Here is my code:

String strValue = "NONE";

<SCRIPT type="text/javascript">
var x=xmlDoc.getElementsByTagName('name');


As you can see in the above code the output of the script will be the 3rd node(starting from 0 to 2). I would like to assign the output of "x[2].childNodes[0].nodeValue" to the variable that was declared in the beginning "strValue".
I have tried different combinations but since the variable needs to be encapsulated with the "<% %>" notation because of JSP, I am stumped.

10-31-2006, 12:21 AM
may i ask what the purpose of this is? knowing why you need to do that would help me better understand what needs to be done, and what can possibly be done as an alternative.

as far as i know anything within <% %> is altered on the server side, so any javascript you have in the source code will only see the output of that, and not what is actually there.

10-31-2006, 12:24 AM
What I am trying to do is read a value from the XML (client side) and included it as a parameter to a function being called on the server-side. I hope this helps.

10-31-2006, 12:39 AM
well from what i know, server side scripts run before the client side is even made.

the only thing i can think of that sounds anything like that would be to redirect the page to another page with the value you want to assign in the location, much like you see on this site.


and where it redirects to is the file that has your server side script that will strip that out and then create the var you want.

10-31-2006, 12:44 AM
basically, post the value to a server side script, then have the server side script create an output from it and then have it do what you wnat it to do after that i.e. create what will be displayed in the clients window.