how to use a Script in two html files


I hope you can help me or correct me if I am wrong .. I would like to know if it is possible to use the same javascript in two different html files, without losing the values of the variables that would be generated from the first tab when using the script. or how can I do so that when the second tab opens (second html file) I use those values that are generated in the script when the first tab is opened (first html file).

and tried to link the same script from the two html files but when the second tab is loaded it is as if the variables were formatted to the predefined ones.

I hope you can give me ideas:)

asked by Oliver Peres 22.12.2017 в 23:25

1 answer


You can use the variables of localStorage (I leave you information here ) with only once assigned its value you can occupy it even in another JS file, this is useful for the browser to save the data and not be lost when reloading the page or changing from one html to another.


//Aqui asigna tu información en la variable localStorage
localStorage.setItem('miNombre', 'Juan')

var nombre = localStorage.getItem('miNombre')

Remember to destroy the localStorage when you no longer occupy it.

answered by 23.12.2017 в 00:23