How to transfer data from one javascript file to another on different pages?

Asked

Viewed 2,073 times

7

I have a simple website with two HTML pages each with a linked Javascript code:

Front Page:

<!DOCTYPE html>
<html>
  <body>
    <script src="primeiraPagina.js"></script>
  </body>
</html>

Second Page:

<!DOCTYPE html>
<html>
  <body>
    <script src="segundaPagina.js"></script>
  </body>
</html>

How I can transfer data from one script file to the other, for example transfer the value of a variable from primeiraPagina.js to the secondPagina.js, without the use of frameworks.

1 answer

9


You can do this in two ways, the first one can be using the sessionStorage. That saves data in the brownser and expire next to it.

//Utilize esse comando para setar o valor na primeira
sessionStorage.setItem('dados', 1);

and

//Utilizar esse comando para recuperar os dados na segunda
sessionStorage.getItem('dados');

You can also pass this data through the url as described in this post

  • 1

    Right around the time I mentioned it, you changed to sessionStorage, hahaha. Just the one I was going to suggest +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.