3

I can't get a javascript inline function call to work. The inline function is this:

<span> <input type="button" value="Previous Week" onClick="subtractOneWeekReloadPage()"> 

I have the function that should be called written into the header as follows:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Time Tracking Page</title>

<script type="text/javascript">
function subtractOneWeekReloadPage() {
<%=currentWeek.subtractOneWeek()%>;
window.location.reload();}

</script>

</head>

When I click on the button on the web page nothing happens though. Neither my jsp call nor the page refresh call is running. Can anyone tell me what I'm doing wrong please?

The relevant output of the page is:

<span> 
<input type="button" value="Previous Week" onClick="subtractOneWeekReloadPage()"> 
Hours for the week of 
<%=currentWeek.firstDayOfThisWeek()%> 
until 
<%=currentWeek.lastDayOfThisWeek()%> 
<input type="button" value="Next Week" onClick="addOneWeekReloadPage()">
</span>

The jsp calls to display the first and last days of the week are working fine. But when I click to subtract one week and reload the page with the newly set week, nothing is happening.

4
  • 3
    what does <%=currentWeek.subtractOneWeek()%>; do? Commented Aug 6, 2012 at 21:31
  • It calls some java methods that creates a new currentWeek object and sets its value. That way when the page is reloaded it should be showing a different current week. It is currently returning an object, though, which must be part of the problem. Commented Aug 6, 2012 at 21:33
  • Give us some example of its output Commented Aug 6, 2012 at 21:34
  • Would this be a situation where I would want to make that method return a new model and view? That would mean I wouldn't need to call the reload page javascript at all. Commented Aug 6, 2012 at 21:38

1 Answer 1

3

The JSP has already run before the page even gets to the browser. If currentWeek.subtractOneWeek outputs anything other than valid JS code, you get a syntax error and the code refuses to run.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I'm beginning to see where I was going wrong. I DO have it returning an object, which I clearly should not be. I just want the method to change the values of the fields the page is querying to fill in its values. But when I had that method with a void return, it didn't allow it to run either.
We can't help without seeing your output as your code looks valid without it.
I've added the output to the original question for you.
@CorayThan Listen to what op says: currentWeek.subtractOneWeek is a server side method, while javascript runs on the client browser. Javascript cannot run serverside java methods like that, and it will be evaluated and replaced by whatever is returned by subtractOneWeek before the client gets it. Say, if it returns 5, your javascript code will look like this to the client: function subtractOneWeekReloadPage() { 5; window.location.reload();}
Yes, I definitely understand what my issue was, and am now trying to accomplish my goals a completely different way. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.