Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using JSP variable in java script  RSS feed

 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i am working ona JSP project where we have a DELETE button. onlick of this button i am calling a java script function like "are you sure you want to delete this user." now i want that the user name should be there in message box like "are you sure you want to delete SACHIN". i have this value (user name) in a java string variable like user_name.

now can someone please guide me how i can put user_name with the message. i try to do this



but my java script stop responding in this case. can someone please help me out!!

regards,

Sachin.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you populating the 'user_name' variable on the server?

Have you viewed the HTML source from your browser to see what is actually coming from the server?
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for bringing my attention on this issue. in my senerio i have foe example 10 recordes as userid and user name and in fron of every button i have an image button like ---



now in each row i have this image and the value attribute consists the user_name. i can pick it from there. but i am not user how to use it with java script. should i use hidden fields?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't answer either of my questions.

How are you populating the JSP variables on the server?
Have you viewed the HTML source from the browser to see if it's being written to the output stream correctly?
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var result=window.confirm("Are you SURE you want to delete <%=user_name%>");

Hi,
try this :


var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);

OR

put the following code in script function

user_name = <%=user_name%>
var result=window.confirm("Are you SURE you want to delete"+ user_name);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by N Chaurasia:

try this :

var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);
...


N Chaurasia,
Are you sure this is what you meant to post?
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya,

try the above code, it works fine


I tried the following with my application



select OK / Cancel and the result is as expected.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not work in the context of the original question. The string will appear outside of the Javascript string literal and be interpreted as a Javascript variable name or cause Javascript syntax errors depending upon what the content of the string variable.
[ July 17, 2006: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:


Will also cause problems.
You are missing both the quotes for the literal and the end of line character ";".

It should be:


If you are going to post code examples to help beginners, it is important that take the time to proof read it (or better, test it by running it) before sending. Otherwise, you may be doing more harm than good.
[ July 17, 2006: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B] hi Ben,
i am using



in my jsp file and it is not causing any problem...




Yes, you're assigning a numeric value to a javascript varible.

In you example code, you've attempted to do the same thing with strings but have't taken the time to quote them properly.
Create a test JSP page that does this and see what happens.

var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);
user_name = <%=user_name%>

[ July 17, 2006: Message edited by: Ben Souther ]
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot reference java variables from javascript directly.
You can however generate javascript from your java/jsp code.

You indicated that you had a list of users, and you could delete any one.
That means you need to include the user name with each line.

Something like this might work

<script>
function confirmDel(userName){
var result=window.confirm("Are you SURE you want to delete " + userName);
return result;
}
</script>

Then somewhere down in a loop

<c:forEach var="user" items="${userList}">
<tr>
<td>${user.name}"</td>
<td><input type="image" name="del_image" on[/i]click="return confirmDel('${user.name}');"> </td>
</c:forEach>

So each line/button generated customizes itself by passing a parameter up to the delete function.
Presumably if you pressed "yes", then the page would submit, and you can determine the id of the user whose delete button you clicked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!