Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assing JSP value to Javascript variable

Status
Not open for further replies.

pouet

Programmer
Jan 31, 2003
3
CA
Hello,

How can I assign the value of a JSP variable(which contains HTML tags) in a Javascript variable ?

I know that JS is for the client and JSP is server side but it may be possible to assign a Javascript variable once ...

I've tried this but it didn't work:

<%
var myJSP = &quot;<FONT FACE=\&quot;Arial\&quot;>Testing...</FONT>&quot;;
%>
<SCRIPT language=&quot;JavaScript&quot;>
function overlap()
{
document.all.mydiv.innerHTML = &quot;<% =myJSP %>&quot;;
}

I also tried to use this:

<INPUT TYPE=&quot;HIDDEN&quot; NAME=&quot;mytest&quot; VALUE=&quot;<% =myJSP%>&quot;>

And then call in my JS function mytest.value but it only works with string but not HTML tags.

Can someone help me please????? I'm lost.....
 
well, I'd try assigning the form variables like you did, but instead of using the &quot;>&quot; and &quot;<&quot; (that break the tag) use &quot;& gt;&quot; and &quot;& lt;&quot; (remove spaces, I added them so you could see it here) that should keep it from breaking the form tag value attribute.

-Scott
 
I think that the problem is that you use
<% =myJSP %> instead of
<%= myJSP %>.
The difference between those two is that the first JSP code is copied directly into the servlet that is generated and tries to perform the statement ' =myJSP'. The second statement prints the value of the variable to the page.

Hope this helps,
Steven.
 
hi all,
how can i assign javascript variables to my jsp variables?

thanx in advance
mukesh
 
You pass the javascript variables over in a HTTP GET or POST request to the JSP. The most common way is by setting the value of hidden HTML fields when the FORM is submitted.

On the JSP side you will have to get the parameters from the HttpServletRequest object. This is easily done:
Code:
<%
String name = request.getParameter(&quot;name&quot;);
%>

Where name is the parameter you are passing. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top