...

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



lmc
10-30-2006, 11:02 PM
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:



<HTML>
<BODY>
<%
String strValue = "NONE";
%>
......

<SCRIPT type="text/javascript">
xmlDoc=loadXMLDoc("customers.xml");
var x=xmlDoc.getElementsByTagName('name');
document.write(x[2].childNodes[0].nodeValue);
</SCRIPT>

</BODY>
</HTML>


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.

brandonH
10-30-2006, 11:21 PM
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.

lmc
10-30-2006, 11:24 PM
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.

brandonH
10-30-2006, 11:39 PM
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.

codingforums.com/newreply.php?do=

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.

brandonH
10-30-2006, 11:44 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum