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

is reference javascript var in JSP possible?  RSS feed

 
Dairmon Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I got a situation which need access javacript var inside JSP, but got the exception
<code>
function getComeInTotal(year){
<%
someFunction(year);
%>
}
</code>
compiler will said:
Undefined variable: year
Is this is possible?
Thanks in advance!!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dairmon
Just to ake sure I have his correct:
The javascript function is 'getComeInTotal' right? And year is a variable you have in the JSP?
When do you get the error? When the JSP is called and beofre the HTML page is rendered? Or is after the HTML page is displayed when you call the ?javascript function?
When you reference year in the first line of the function:
function getComeInTotal(year){
The year variable should be passed into the script from the HTML page that calls the function, if you are trying to use the year variable somewhere else in the function it'll be null if you don't actually pass one into it.
If the error is in the other line:
someFunction(year);
This is executed servers side by the JSP before the HTML is rendered and the variable year must be defined in the JSP before it is used here. If the variable year is not defined prior to that line then you'll get the error you described.
One of those two situations is causing the error. If you can't figure it out post some more code.
hope that helps
 
Dairmon Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
Thank you very much for the comments!
Actually I got the complier problem when the container load the page (I am using tomcat 4.0).
The "year" actually is the reference in javascript
<code>
<html>
<head>
<SCRIPT>
function fn2(year){
<%
out.println(year);
%>
}
</SCRIPT>
</head>
<body>
<form name=form>
<input type=text onchange="fn2('2002')">
</form>
</body>
</html>
</code>
following is the error message!
jsp.java:59: Undefined variable: year
out.println(year);
^
Actually I was wondering whether this is possible?
Thanks!
 
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
It appears to me that you have a basic (and common) misunderstanding of how JSP works.
All of the Java scriptlets and other JSP elements are evaluated on the server in order to create a (usually) normal, "every day" HTML page that is then sent to the browser.
Your javascript function, which defines the javascript parameter 'year', is executed on the client side by the browser in response to whatever client stimulus triggers it off.
Therefore, the answer to your question is 'no'. The Java on a JSP has no knowledge of your javascript identifiers -- in fact, they don't even exist in the same time/space continuum (if you get my drift).
hth,
bear
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear is on the money with his explanation. There is no way u can access a java script variable in your jsp code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!