2

I have a JS script inside a HTML file that get a certain value from API and put on a variable. I want to put that same variable with the value on another HTML file (The new HTML file is loaded from a button in the previous page). Is there any way to to that?

Gabriel
  • 79
  • 8
  • 1
    Try out sessionStorage: `sessionStorage.setItem('key', variable);` will store the variable value in the browser session storage. `sessionStorage.getItem('key');` will return the set value of said variable. See: https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage – Andre Jun 10 '20 at 23:53
  • 1
    You could maybe get away with assigning it to `window`? Not sure how viable that is, though. Take with a grain of salt. – matthew-e-brown Jun 11 '20 at 00:22

1 Answers1

1

I can think of two easy ways.

1.) Try out sessionStorage: sessionStorage.setItem('key', variable); will store the variable value in the browser session storage. sessionStorage.getItem('key'); will return the set value of said variable.

See: https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage

2.) Add the variable value to your url www.yourdomain.com?key=value and retrieve it from your second script.

const urlString = window.location.href;
const url = new URL(urlString);
const value = url.searchParams.get("key");

See: How to get the value from the GET parameters?

Community
  • 1
  • 1
Andre
  • 4,185
  • 5
  • 22
  • 32