0

On click button event will call a function to generate some random number:

  <script>
function connectPoints()
{
 var point_add = RANDOM NUMBER;
 createArray(point_add); 
}

function createPolyline(point_add)
{
 var totalRandomNumbers= oldPoint + point_add;
}

Now after client is happy with his generation of random numbers he will hit saveNumbers button

<input type=button name="saveNumbers" value="Save" onclick="saveNumbers();" />
function saveNumbers(){
Here I need to get the values of totalRandomNumbers
}

I need some mechanism like storing the value of totalRandomNumbers in session so that I can retrieve in this new function saveNumbers().

4
  • Does the location change for the user or does he get to stay on the same page? Also see this Commented Jan 27, 2014 at 10:51
  • the user will be on same page, there will not be a page refresh Commented Jan 27, 2014 at 10:54
  • define var totalRandomNumbers globally Commented Jan 27, 2014 at 11:00
  • possible duplicate of Define global variable in a JavaScript function Commented Jan 27, 2014 at 11:02

1 Answer 1

1

Solutions

localStorage

is deleted when user clears the browser cache

window.localStorage['randomNumber']=19;
var randomNumber=window.localStorage['randomNumber']*1;
//or using an array
window.localStorage['data']=JSON.stringify({randomNumber:19,total:1});
var randomNumber=JSON.parse(window.localStorage['data'])['randomNumber'];

sessionStorage

is deleted when user closes the page

it has slightly less compatibility than localStorage

and the example is already posted in your comments.

global variable

is deleted when user closes the page or changes the location

<script>
var randomNumber;//global variable
window.onload=function(){
 function changeRandomNumber(){
  randomNumber=19;
 }
 changeRandomNumber();
 //rest of you code.
}
</script>

as you don't refresh or change location this is the best solution.

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.