0

I am curious as to how I should be passing a variable from my .jsp page to my JavaScript file. Currently I have the following...

<c:forEach items="${stats}" var="s" >
        <script type="text/javascript">
            var status = "hello";
        </script>
            <div class='memory' id='memory'>
                <p id='text'>Memory<p>
            </div>
            <div class='memory1' id='memory1'>
                <p id='text'> Memory 2</p>
            </div>  
             <script  src="js/temp.js" type="text/javascript"></script>
        </c:forEach>

This works just fine for alert(status); However I am wondering how I should go about passing the field of {$s.status} to the javascript file as changing

<script type="text/javascript">
    var status = {$s.status};
</script>

does not work

3
  • You mean dynamically change with the server value? ,,, then that is not possible unless it either gets pushed to the client/browser, or the client asks for it, using e.g. an Ajax call. Commented Jul 9, 2018 at 14:22
  • the idea is yes the value will be changing as per a defined refresh rate. I am retrieving statistics about another running application such as memory used and status, and need to draw a progress bar on the screen. I am wondering then what the best way would be to go about this as it sounds like the way I originally wanted is a bit convoluted... Commented Jul 9, 2018 at 19:15
  • Ok, so what I suggested is a couple ways, and yet another is websockets, but again, which is based on how/what your are going to do. Commented Jul 9, 2018 at 19:18

3 Answers 3

3

Another way to do it:

<input type="hidden" value="${s.status}" id="status">
<script>
   var status = document.getElementById('status').value;
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do it is to use something like:

var status = '${s.status}';

Comments

0

Try this:

var test = <%=request.getParameter("test")%>;

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.