• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the Java variable inside the Javascript function

 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I wanted to know how can we use the variable declared and defined in Java inside the javascript function?

Suggestion required....


Example
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Not directly.

JSP executes on the server; JavaScript on the client. There can be no interaction.

However, remember that the purpose of a JSP is to dynamically create the markup to send to the client, so you can use JSP to hard-code the variable's value into the JavaScript:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find this article informative.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sir, The article really clarifies most of the doubts.
One question is Whether in JSP by writing one script function can we write the complete JAVA code inside the jsp file withing <% %> tags
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put Java code in a JSP, but modern best practices say that you should not.

I have another article that you might find useful.
 
Paulina Tanner
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a similar problem.

I would like to pass a java variable as a parameter in to a onKlick event handler javascript function. How do I do that?

<html>
....
.
..
.
<script>

function enable(value){
alert(value);
}
</script>


<%
int i=89;
%>

<input type=checkbox name="chkBox" onKlick="enable(<%=i%>);"


How do I pass the value of i to the enable function?

Please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about your example doesn't work? The value of I should be written into the page before it is sent to the browser.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a similar problem.

I would like to pass a java variable as a parameter in to a onKlick event handler javascript function. How do I do that?

<html>
....
.
..
.
<script>

function enable(value){
alert(value);
}
</script>


<%
int i=89;
%>

<input type=checkbox name="chkBox" onKlick="enable(<%=i%>);"


How do I pass the value of i to the enable function?

Please help.



Hi I have resolved my issue to get the value in a javascript but I dint know whether it is the best way to that or nat.
I am giving the approach.
1.Taken the java variable and assign it the value
2.Now i have assigned this value to the Hidden parameter of the form
3 And from the javascript function i used the value of this parameter.

In my case it works fine...
Please suggest whether any other approach can be possible
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulina Tanner,
In the future, please start a new thread with your question.
Asking a new question in someone else's thread is called hijacking and is a rude thing to do to the original poster.
If you think your question is similar or related to another, add a link to that thread in your own.

Thanks
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic