-1

How can I use a variable that is creats in first.js in two.js? Notice: the two files are linked to two different html pages.

I didn't try anything cause I don't have any idea Notice: I can't use the same js file because there's function that work for the first htmls file but I don't want them to work for the second one,that's why I'm using two js files

Rayzel
  • 31
  • 5
  • How are these two different html pages related to each other? – Teemu Dec 27 '22 at 12:29
  • They are not,after completed some steps in the first html file, a button take the user to the second,but I need some variables that I took from the first page to use in the second one. – Rayzel Dec 27 '22 at 12:31
  • 1
    Does this answer your question? [Passing javascript variables between pages](https://stackoverflow.com/questions/11581543/passing-javascript-variables-between-pages) – evolutionxbox Dec 27 '22 at 12:33
  • You can use [Sesson Storage](https://www.w3schools.com/jsref/prop_win_sessionstorage.asp) for pertist value across multiple pages. – Hiren Patel Dec 27 '22 at 12:40

3 Answers3

0

You can do it by exporting that variable and import it into another file

for exa- first.js

export const message = "Hello"; 

two.js

import { message } from "./utils.js";
vimuth
  • 5,064
  • 33
  • 79
  • 116
0

You can use SessionStorage for persist value acros multiple pages.

You can save value using:

sessionStorage.setItem("Name", "Hiren");

You can get value using:

sessionStorage.getItem("Name");
Hiren Patel
  • 1,071
  • 11
  • 34
0

You can't as soon as you leave first html page script reload and everything is set back to initial value.

Some workarounds are using localStorage/sessionStorage

Or you can create SPA(single page application) using react or vanilla js