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().

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2093576
  • 3,082
  • 7
  • 32
  • 43

1 Answers1

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.

cocco
  • 16,442
  • 7
  • 62
  • 77