0

I have created variable in javascript, which I need to access inside if condition.

Below code is not working, I'm not able to access variable created in javascript, getting blank value.

<script>
var isComplete= "No";
function startup(){
 // some code which changing value for isComplete variable to "Yes";
}
</script>

<c:choose>
        <c:when test="${isComplete == 'No'}">
            <td>&nbsp;</td>
        </c:when>
        <c:otherwise>
            <td><a href="JavaScript:send('<c:out value="id"/>')">Send</a></td>
        </c:otherwise>
    </c:choose>

I'm getting value of isComplete variable blank. How can I access it?

3
  • A classic one: The key is to realize, where and when which code is executed - JSP on the server, when the page is requested and rendered (i.e. before the response is sent to the browser), and Javascript in the browser, after the browser receives the already generated response. Commented Oct 13, 2017 at 12:57
  • Please check code once, I'm performing operations in the startup function. there is no any case/condition of before or after response Commented Oct 13, 2017 at 13:01
  • There is simply NO WAY for the JSP to access Javascript variable within the same page. Your startup() function runs inside the browser, after the server has already finished processing the JSP. At the time of JSP processing, there is simply no isComplete JS variable yet, so you cannot access it. Commented Oct 13, 2017 at 13:07

1 Answer 1

0

JSP code is executed on the server, whereas javascript code is executed in the browser, after the server has finished processing the JSP. Even the startup() in javascript is run in the browser some time after the JSP has finished execution.

If the purpose of accessing a javascript variable is to conditionally modify the html generated by the JSP, you can refactor your code to put the modifications in javascript as well, so it would run in the browser, and re-write the html as needed. This SO answer shows an example of how you might do it.

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

Comments

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.