This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes How do I intialise the java script varible to the JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "How do I intialise the java script varible to the JSP" Watch "How do I intialise the java script varible to the JSP" New topic
Author

How do I intialise the java script varible to the JSP

Mastan Rao
Greenhorn

Joined: Nov 26, 2007
Posts: 10
Hi All,
can some one help for this...
I have to initialise javascript varible to JSP variable
I have a code like this
<script>
fun(n)
{
<% x=%>{Here I need to initialise the n to x)

Thanks in advance

Mastan
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hint, a JSP expression prints the value to the page.
<%="Hello"%> will print "Hello".

Use your browser's "view source" feature to see what is really being printed to the page.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
Maybe something like this, assuming that x is a numeric value. If it's a string, add enclosing quotes.

<script>
fun(n)
{
n = <%= x%> ;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63208
    
  69

JavaScript executes on the client long after the JSP has executed. So there can be no interaction between them. You may find this article helpful.

But remember that JavaScript is just template text to the JSP, so you can use the JSP to create the JavaScript.

So if you need the value in JSP variable x to be "passed" to a JavaScript function:


If the value of x is 213, this will result in the following on the HTML page:



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63208
    
  69

Bang, bang and bang!
 
Have you checked out Aspose?
 
subject: How do I intialise the java script varible to the JSP
 
It's not a secret anymore!