Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning a Java variable to a javascript variable  RSS feed

 
BhanuPrakash Vemula V
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folk,

If i want to assign a Java variable to a Java script variable in my application. Is it possible. If possible, let me know.

Regards
Bhanuprakash.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"BhanuPrakash Vemula V",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. All you'll need to do to comply is to remove the trailing "v".

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That greatly depends upon what you mean by that. Please supply more details.

Perhaps read this.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's possible through EL. But if it is, what I would be doing is like this:

1. Assign a request-scoped variable

int myvar = 0;
request.setAttribute("myvar", myvar);

2. Use EL for javascript to use the request-scoped variable

document.write(${myvar});

That's what I'm thinking... But I might just be dreaming... lolz
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it should be a problem if you want to set the variable when the page is loaded.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JSP forum.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Rutgers:
I don't think it should be a problem if you want to set the variable when the page is loaded.



Is it possible to do reverse?

I mean, assigning JS variable into Java variable.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to send a value from the client to the server is to make a request, and send the value as a request parameter.

The most common way is to either append the value to a dynamic url
ie


If you are dealing with a form, then a hidden field on the form can be used.
Normally you catch the onclick of a submit button, or onsubmit of the form, and run javascript to copy the value of the javascript variable into the hidden field before the form gets submitted.

Of course you can only pass Strings as parameters, so it does have its limitations.

Cheers,
evnafets
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rathi,
Please don't hijack other people's threads.
If you have a question, start your own thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!