How to use localStorage to save variable with random value
<p>I'm trying to use localStorage to save a variable with value generated randomly from an array in a JavaScript file, and pass it to another HTML file. However, the value in Javascript file (<code>Random_Msg</code>) and the value in HTML file (<code>Random_Msg1</code>) are not the same, means it's not saved, instead it generated randomly another value.</p>

<p>These are the code to generate variable and save in localStorage:</p>

<pre><code>function CreateRandomMsg(){
var RandomMsg = Msgs_Arr[Math.floor(Math.random()* Msgs_Arr.length)];
return RandomMsg;
}

var Random_Msg = CreateRandomMsg();

function alertMsg(){
alert(Random_Msg);
}
window.localStorage.setItem("Random_Msg1",Random_Msg);
</code></pre>

<p>In my HTML file, I just retrieved the variable first:</p>

<pre><code>var Random_Msg1 = window.localStorage.getItem("Random_Msg1");
</code></pre>

<p>And use it in if statement:</p>

<pre><code>if (Random_Msg1 == Msgs_Arr[0] || Random_Msg1 == Msgs_Arr[1]){
     value = facesDet.photos[0].tags[0].attributes.glasses.value;
     confidence = facesDet.photos[0].tags[0].attributes.glasses.confidence;
    } else if (Random_Msg1 == Msgs_Arr[2] || Random_Msg1 == Msgs_Arr[3]) {
      value = facesDet.photos[0].tags[0].attributes.smiling.value;
      confidence = facesDet.photos[0].tags[0].attributes.smiling.confidence;
    };
</code></pre>