| Author |
Use a JSP variable in JavaScript? Plz Help
|
Daniel Iszatt
Greenhorn
Joined: Feb 08, 2003
Posts: 4
|
|
Firstly I am new to JavaScript. I have a JSP page. There is a chunk of JSP code that creates a String variable. On this page there is also a chunk of code that I was given with encode and decode functions. These functions take a pair of strings and encrypt them. 1 Of the stings that I need to use in this function is created via JSP code. So I need to get this JSP String variable to work in the encode function. I know how to display JSP strings in HTML - but I dont know how to use them in JavaScript. Is it possible? Also with the result of the encode function that is in JavaScript - I need to send the result in a form on that page. My second question is how do you access JavaScript variables in HTML so that I can send it via a hidden value in a form?
|
 |
Suresh Selvaraj
Ranch Hand
Joined: Nov 14, 2000
Posts: 104
|
|
Hi, 1. <% String s = "Java"; %> <script> ... document.[formName].[fieldName].value="<%= s %>"; ... </script> 2. <% String x1 = null; %> <script> ... var s1 = "JAVA"; if(s1 != "") { <% x1="Any Computed Value or JAVA"; %> } ... </script> <INPUT TYPE=HIDDEN NAME="Counter" VALUE="<%=x1%>"> Hope this helps. Suresh Selvaraj Sun Certified Java Programmer
|
Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
|
 |
Daniel Iszatt
Greenhorn
Joined: Feb 08, 2003
Posts: 4
|
|
Thankyou for that. It looks good. I'll try it out next time I'm at work.
|
 |
jini jane
Greenhorn
Joined: Nov 06, 2002
Posts: 4
|
|
hi, The method of embedding the scriplet in the script is fine but when a user views the code it is clearly visible. in case the javascript is in a file how do we access a bean ?
|
 |
Pete Harris
Ranch Hand
Joined: Feb 05, 2003
Posts: 39
|
|
There is no way of accessing a java bean from javascript as the bean is a sever side entity, whereas the javascript runs on the browser once the server request has been completed. The only way of including jsp values in an included file is either to make the.js file a .jsp and then including it exactly as if it were a normal javascript file, or by declaring and defining variables within script tags in your main JSP, and referencing them in the included javascript file.
|
 |
jini jane
Greenhorn
Joined: Nov 06, 2002
Posts: 4
|
|
|
Thanks Pete.
|
 |
 |
|
|
subject: Use a JSP variable in JavaScript? Plz Help
|
|
|