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

7

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

First 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 can I transfer data from one script file to another, for example transferring the value of a firstPage.js variable to the second

SecondPage.js , without use of frameworks .

    
asked by anonymous 05.10.2015 / 03:54

1 answer

9

You can do this in two ways, the first one you can use by sessionStorage . It saves the data in the brownser session and expires 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

05.10.2015 / 04:18