title: Need help sharing a Javascript variable locally between files
body: <p>I've been developing a speed test typing game for some time now, and I've been stuck on how to take the time you finish from one HTML page (the game itself) to an alternate one (the score page). I've been trying to use import{} export{}, but I'm not 100% sure how they function and so they haven't exactly worked. I'll attach both the game HTML and Javascript code and the score HTML and Javascript code.</p>
  <pre><code>&lt;!-- THIS IS THE HTML PAGE FOR THE GAME --&gt;
  &lt;!DOCTYPE html&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
          &lt;head&gt;
              &lt;meta charset=&quot;utf-8&quot;&gt;
              &lt;title&gt;Typeboss || Speed Typing Web Game&lt;/title&gt;
              &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/gamestyle.css&quot;&gt;
              &lt;style&gt;
                  @import url('https://fonts.googleapis.com/css2?family=Cabin:wght@500&amp;display=swap');
              &lt;/style&gt;
              &lt;script src=&quot;../js/gamescript.js&quot; defer&gt;&lt;/script&gt;
              &lt;script src=&quot;../../main/js/mainscript.js&quot; type=&quot;module&quot; defer&gt;&lt;/script&gt;
              &lt;script src=&quot;./scorescript.js&quot; type=&quot;module&quot; defer&gt;&lt;/script&gt;
  
          &lt;/head&gt;
  
          &lt;body&gt;
              &lt;div id=&quot;all&quot;&gt;
                  &lt;div id=&quot;stats&quot;&gt;
                      &lt;h1 id=&quot;bossHealth&quot; class=&quot;stat&quot;&gt;BOSS HEALTH:&lt;/h1&gt;
                      &lt;h1 id=&quot;time&quot; class=&quot;stat&quot;&gt;&lt;span id=&quot;minutes&quot;&gt;0&lt;/span&gt;:&lt;span id=&quot;seconds&quot;&gt;00&lt;/span&gt;&lt;/h1&gt;
                  &lt;/div&gt;
  
                  &lt;div id=&quot;boss&quot;&gt;
                      &lt;img src=&quot;../imgs/boss.png&quot; width=&quot;400px&quot; height=&quot;400px&quot; id=&quot;bossSprite&quot;&gt;
                  &lt;/div&gt;
  
                  &lt;div id=&quot;input&quot;&gt;
                      &lt;h3 id=&quot;outcome&quot;&gt;&lt;/h3&gt;
                      &lt;center&gt;&lt;p id=&quot;prompt&quot;&gt;&lt;/p&gt;&lt;/center&gt;
                      &lt;textarea id=&quot;userInput&quot; name=&quot;TypeBox&quot; rows=&quot;4&quot; cols=&quot;100&quot; autofocus&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  <pre><code>// THIS IS THE JAVASCRIPT CODE
  let scoreTime;
  var seconds = 0;
  var minutes = 0;
  let score = 0;
  
  const userInput = document.getElementById(&quot;userInput&quot;);
  const promptBox = document.getElementById(&quot;prompt&quot;);
  const outcomeText = document.getElementById(&quot;outcome&quot;);
  const bossHealthtText = document.getElementById(&quot;bossHealth&quot;);
  const timer = document.getElementById(&quot;time&quot;);
  const bossSprite = document.getElementById(&quot;bossSprite&quot;);
  
  const secondsText = document.getElementById(&quot;seconds&quot;);
  const minutesText = document.getElementById(&quot;minutes&quot;);
  
  let gameActive = true;
  
  
  
  document.addEventListener('keypress', function(event){
      if (event.key === 'Enter'){
          checkInput();
      }
  });
  
  
  let bossHealth = 100;
  
  let multiplier = 10;
  let mistakes = 0;
  
  //
  const prompts = [&quot;How much wood could a woodchuck chuck if a woodchuck could chuck wood?&quot;, &quot;I will not lose another pilot.&quot;, &quot;I can't find my keys!&quot;, &quot;Live as if you were to die tomorrow.&quot;, &quot;Toto, I've got a feeling we're not in Kansas anymore...&quot;, &quot;May the force be with you.&quot;, &quot;If our lives are already written, it would take a courageous man to change the script.&quot;, &quot;An apple a day keeps the doctor away.&quot;, &quot;Frankly, my dear, I don't give a damn.&quot;, &quot;Hello, world!&quot;, &quot;My favorite sport is basketball!&quot;, &quot;Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.&quot;, &quot;You only live once, but if you do it right, once is enough.&quot;, &quot;I'm the fastest typer in the wild west!&quot;, &quot;Frosty the snowman!&quot;, &quot;I'm not a big fan of ice cream.&quot;, &quot;If birds can fly, then we should too.&quot;, &quot;I don't like trucks, they look weird.&quot;, &quot;I love typing!&quot;, &quot;I'm a rocket man, burning on a fuse out there alone.&quot;, &quot;The water was blue and the skies the same, it was summer.&quot;];
  
  //GAME INIT
  
  bossHealthtText.innerHTML = `BOSS HEALTH: ${bossHealth}`;
  
  function getNewPrompt() {
      promptChoice = Math.floor(Math.random() * prompts.length);
  
      let previousPrompt = prompts[promptChoice];
      if (previousPrompt === prompts[promptChoice]){
          promptChoice = Math.floor(Math.random() * prompts.length);
          console.log(&quot;COPY OFF&quot;);
      }
      promptBox.innerHTML = prompts[promptChoice];
  }
  
  
  function checkInput(){
      if (userInput.value.trim() === promptBox.innerHTML.trim()){
          outcome.innerHTML = &quot;Correct&quot;;
          bossHealth -= 10;
          bossHealthtText.innerHTML = `BOSS HEALTH: ${bossHealth}`;
          let hitSprite = Math.floor(Math.random() *2);
  
          if(hitSprite === 0){
              bossSprite.src = &quot;../imgs/bosshit/bosshit1.png&quot;;
          }
          if(hitSprite === 1){
              bossSprite.src = &quot;../imgs/bosshit/bosshit2.png&quot;;
          }
  
          setTimeout(function(){
              bossSprite.src = &quot;../imgs/boss.png&quot;;
          }, 1000);
  
          if (bossHealth &lt;= 0){
          console.log(&quot;WORKING&quot;);
          window.location.href = &quot;./score.html&quot;;
          createScore();
      }
  
          getNewPrompt();
          userInput.value = null;
      }
      else{
          outcome.innerHTML = &quot;Try again...&quot;;
          userInput.value = null;
          multiplier --;
          mistakes ++;
          return; 
      }
  }
  
  function gameTimer(){
      console.log(&quot;working&quot;);
      setTimeout(function(){
          setInterval(function(){
              seconds++;
              if(seconds === 60){
                  minutes ++;
                  seconds = 0;
              }
          if (seconds &lt; 9){
              secondsText.innerHTML = &quot;0&quot; + seconds;
          }
          if (seconds &gt; 9){
              secondsText.innerHTML = seconds;
          }
          if(minutes &lt; 9){
              minutesText.innerHTML =  minutes;
          }
          if(minutes &gt; 9){
              minutesText.innerHTML = minutes;
          }
  
          if (seconds &lt; 9){
              scoreTime = minutes + &quot;:&quot; +  &quot;0&quot; + seconds;
          }
          if (seconds &gt; 9){
              scoreTime = minutes + &quot;:&quot; + seconds;
          }
  
          timePoints = minutes + seconds * 2;
  
          return scoreTime;
          return timePoints;
  
          }, 1000)
          
      }, 1000);
  
  }
  
  
  function game(){
      gameTimer();
      getNewPrompt();
      createHeart();
  }
  
  function createScore(){
      let bonus = Math.floor(Math.random() * 500);
      score = timePoints * multiplier + bonus;
  
      export { score };
  }
  
  
  game();
  
  </code></pre>
  <pre><code>&lt;!-- THIS IS THE SCORE HTML PAGE --&gt;
  &lt;!DOCTYPE html&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html onclick=&quot;celebrate()&quot;&gt;
          &lt;head&gt;
              &lt;meta charset=&quot;utf-8&quot;&gt;
              &lt;title&gt;Typeboss || Your Score!&lt;/title&gt;
              &lt;link rel=&quot;stylesheet&quot; href=&quot;../css/scorestyle.css&quot;&gt;
              &lt;style&gt;
                  @import url('https://fonts.googleapis.com/css2?family=Cabin:wght@500&amp;display=swap');
              &lt;/style&gt;
          &lt;/head&gt;
          &lt;body&gt;
              &lt;center&gt;&lt;h1 style=&quot;color: whitesmoke;&quot; id=&quot;header&quot;&gt;YOU BEAT THE BOSS!&lt;/h1&gt;&lt;/center&gt;
              &lt;center&gt;&lt;p&gt;(CLICK FOR VICTORY MUSIC)&lt;/p&gt;&lt;/center&gt;
  
              &lt;h3 id=&quot;time&quot;&gt;TIME:&lt;/h3&gt;
              &lt;p id=&quot;playertime&quot;&gt;&lt;/p&gt;
  
              &lt;h3 id=&quot;score&quot;&gt;SCORE:&lt;/h3&gt;
              &lt;p id=&quot;playertime&quot;&gt;&lt;/p&gt;
  
              &lt;h3 id=&quot;mistakes&quot;&gt;MISTAKES:&lt;/h3&gt;
              &lt;p id=&quot;playertime&quot;&gt;&lt;/p&gt;
  
              &lt;h2 id=&quot;highscore&quot;&gt;&lt;/h2&gt;
  
              &lt;a href=&quot;../../main/html/main.html&quot;&gt;&lt;button id=&quot;redirect&quot;&gt;BACK TO HOME&lt;/button&gt;&lt;/a&gt;
          &lt;/body&gt;
          &lt;script type=&quot;module&quot; src=&quot;../js/gamescript.js&quot; defer&gt;&lt;/script&gt;
      &lt;/html&gt;
  </code></pre>
  <pre><code>// AND HERE IS THE SCORE'S CODE
  winMusic = new Audio();
  winMusic.src = &quot;./winmusic.mp3&quot;;
  
  function celebrate(){
      winMusic.play();
      winMusic.loop = tru
  }
  
  import { score } from &quot;./gamescript.js&quot;;
  const scoreText = document.getElementById(&quot;score&quot;);
  
  scoreText.innerHTML = score + &quot; points!&quot;
  </code></pre>
  <p>I hope I can get this issue resolved, and I hope this post was clear enough to understand. This is my first post on stack so please do let me know if I formatted it wrong, and any feedback that could make it easier to understand in the future. Thank you in advance and have a good day.</p>