Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing the value of a parameter from java to javascript  RSS feed

 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I am doing:

I have a JSP in whhich I display rows for a table (picking the data up from the database). This code for generating the table rows is in a java class. After its displayed, I want to make a select on a row and delete it. What I am tryin to do here is that I am generationg an id and linking that id with each row. This id comes back as a request parameter to the same jsp. I am trying to get the ids value in a javascript fiunction on the jsp. But it dosesn seem to work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "it doesn't work" mean? How are you trying to get the value into javascript? Without details it's impossible to even begin to guess what you are trying to do or what the issue may be.
 
Scott Selikoff
author
Bartender
Posts: 4081
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the question in your topic (because I don't follow your post) you can 'convert' a JSP variable (lets use a java.lang.String as an example) to a Javascript variable very easily:

Then, anywhere in the javascript, just call myJavaScriptVariable and if the page is finished loading, the variable will be available.
 
Saskia de Jong
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:

Then, anywhere in the javascript, just call myJavaScriptVariable and if the page is finished loading, the variable will be available.



That's only going to work for very basic value. In general you need to escape you Java variable before assigning its value to the javascript var. Don't forget that the assignment happens by literally printing the Java var's value to the page.

As a starting point you could use the following code:



then in your bean, do something like:



and finally, on the JSP page:



of course, for numbers you don't use the single quotes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!