• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

How to use the Java variable inside the Javascript function

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find this article informative.
 
Rizwan Patel
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic