I hope I understood your question correctly. To persist data you can use SessionStorage. It can only persist JSON values, and it get emptied once a user have closed the browser window. If you don't like this behavior, you can use LocalStorage, which saves data across sessions.
Here's JS:
function onRedirect() {
sessionStorage.setItem("values", JSON.stringify(getValues()));
sessionStorage.setItem("values2", JSON.stringify(getValues2()));
}
function getValues() {
return [1, 2, 3];
}
function getValues2() {
return ["hello", "world"];
}
function renderList(arr) {
return arr.map(item => `<li>${item}</li>`).join("");
}
document.querySelector("button").addEventListener("click", onRedirect);
if (window.location.pathname === "/test2.html") {
const values = JSON.parse(sessionStorage.getItem("values"));
const values2 = JSON.parse(sessionStorage.getItem("values2"));
document.querySelector(".values").innerHTML = renderList(values);
document.querySelector(".values2").innerHTML = renderList(values2);
}
and here's HTML:
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<button class="button" type="button" onClick="javascript:window.location.href='test2.html'">
Home
</button>
<ul class="values">
</ul>
<ul class="values2">
</ul>
<script src="src/index.js"></script>
</body>
</html>
Codesandbox:
https://codesandbox.io/s/km2o2xv3pr
localStorage()developer.mozilla.org/en-US/docs/Web/API/Window/localStorage