Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to pass variable in jsp to java script function ?

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
How I need to pass a variable of JSP page to java script function as param.

How to do that? I tried below approach , but doesnot work out.
message is varible name
confirmLogOut is java script function

on click="return confirmLogOut(<%=message%>)


Could any of you give me some hints?

Thanks in advances

Sorry I have to put space between "on click". other wise i could submit this post


[ October 14, 2007: Message edited by: walter wang ]

[ October 14, 2007: Message edited by: walter wang ]
[ October 14, 2007: Message edited by: walter wang ]
 
author & internet detective
Posts: 40213
818
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Walter,
Note that you are not passing the message variable. Rather you are passing the value of the message variable. Since it is a string, you need to add the quotes to make JavaScript happy:

return confirmLogOut('<%=message%>')
 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeanne,

Thanks for your reply, after tring your appraoch
I found that,
it prints out " <%=message%> " as one whole string instead of content of it.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this did not work?

on click="return confirmLogOut("<%=message%>")"
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you cannot pass a JSP value to JavaScript as the JSP is executing on the server and JavaScript executes on the client. Perhaps this article can help with that concept.

As Jeanne pointed out, you can write the value of the variable into the HTML but you need to make sure that the markup is correct. Do a View Source of the response sent to the browser to see what's being constructed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic