aspose file tools*
The moose likes JSP and the fly likes How to pass variable in jsp to java script function ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass variable in jsp to java script function ?" Watch "How to pass variable in jsp to java script function ?" New topic
Author

How to pass variable in jsp to java script function ?

walter wang
Ranch Hand

Joined: Jun 02, 2001
Posts: 154
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 ]

public class Walter { public boolean is_Working_Now (boolean is_boss_Coming) { return is_boss_Coming; }
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

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%>')


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
walter wang
Ranch Hand

Joined: Jun 02, 2001
Posts: 154
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.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
And this did not work?

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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to pass variable in jsp to java script function ?