File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I intialise the java script varible to the JSP

 
Mastan Rao
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bang, bang and bang!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic