3

I need to save the value of a variable in Javascript which is declared and assigned value during previous call to the function in javascript. I am both declaring and initializing the variable only once, but I need to get the value of a variable when I needed. Is there is any way to store the value of variable and retrieve it, any time as I needed??

p1.jsp

<table>
     <% 
       for(i=0;i<g;i++){
           request.getSession().setAttribute("incr",i);
     %>
     <tr><td><jsp:include page="/p2.jsp" /></tr></td>
     <%}%>
</table>

p2.jsp

 <%!int i;%>
    <%
      i=request.getSession().getAttribute("incr");
    %>
    <script type='text/javascript'>
    var k<%=i%>;
    function d(){
         k<%=i%>=5;
    }
    </script>

//Here, i need to print k0,k1,.... value as i needed

5
  • 1
    please share your code Commented Jul 11, 2013 at 5:32
  • 1
    Are you trying to use this variable only within the duration that one particular browser page is displayed or do you want this value to persist across multiple page loads. The easiest answer is very different depending upon the answer to that question. Commented Jul 11, 2013 at 5:37
  • I need to save my value across multiple pages Commented Jul 11, 2013 at 5:42
  • What browsers do you need to support? Commented Jul 11, 2013 at 5:43
  • IE and Chrome browsers Commented Jul 11, 2013 at 5:46

1 Answer 1

1

Use sessionStorage or localStorage to save the data as you need. For example:

sessionStorage.setItem("variable",value);
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.