Forums Register Login

Javascript accessing jsp variable

+Pie Number of slices to send: Send
Hi,


In my jsp I have a string variable, the javascript in that jsp should access that varialbe, don't know how I can do it? Any help. Thanks.

a.jsp

String year = (String)session.getAttribute("YEAR");

<script>
function test(){

//access year
}
</script>
+Pie Number of slices to send: Send
JavaScript can't access the JSP variable, because that exists on the server when the page is generated. You can embed the value of that variable in the JavaScript code. Something like the following should do the trick.

Leave out the double quotes if the value is numerical.
+Pie Number of slices to send: Send
Great. Thanks a lot. Let me try that.
+Pie Number of slices to send: Send
Be sure that you understand why that works. Do a View Source on the page as sent to the browser, and you will see that the value was substituted on the server before the browser gets the page.

You may also find this article helpful.
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5683 times.
Similar Threads
how can i put java script value into jsp variable?
Problem with script tag
Problem with closing script tag
questions about JSTL variables
Intialize javascript variable using jsp
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:12:12.