wood burning stoves*
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
Marshal

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

<script>
fun(n)
{
n = <%= x%> ;


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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: 60782
    
  65

Bang, bang and bang!
 
 
subject: How do I intialise the java script varible to the JSP
 
Similar Threads
Assign Javascript value to JSP var
while-loop
How to pass variable in jsp to java script function ?
Sharing array with Javascript in JSP?
a question about a drop-down menu