my dog learned polymorphism*
The moose likes JSP and the fly likes passing jsp variable to a javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "passing jsp variable to a javascript function" Watch "passing jsp variable to a javascript function" New topic
Author

passing jsp variable to a javascript function

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hello,
I have JSP page in which I pass a java value to a JavaScript function. A value is a name of a page passed to JavaScript window pop-up function. I get an error when I try to do it:
jsp page:
---------
String hlpFile = "test";
.
.
out.println("<a href=\"javascript:loadWin(" + hlpFile + ")\" class=\"nav\">" + prog + "-" + hlpFile + "</a>\n");

JavaScript function:
--------------------
function loadWin(file){
helpWin = window.open("generic_help/"+file,"help","height=650,width=800,resize=1,scrollbars=yes,top=10,left=100");
}
In browser's JavaScript "console" I get:
Error: 'test' is undefined
Any suggestions why this does not work?
thanks,
Alex


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5

out.println("<a href=\"javascript:loadWin(" + hlpFile + ")\" class=\"nav\">" + prog + "-" + hlpFile + "</a>\n");

Try to add quotes around the hlpFile variable:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
yup, that did it. Thanks!
 
 
subject: passing jsp variable to a javascript function
 
Similar Threads
Assigning JSP variables to an IF statement
Scriptlets in Struts
Calling javascript function in js file from JSP
JSF with fileupload and iframe
JSTL EL language not resolving