简体   繁体   中英

Pass array in JavaScript to another page without using sessionStorage or localStorage

有谁知道如何在不使用sessionStoragelocalStorage情况下将array值传递给另一个页面?

I take it that you have a page; you want to send some data to another page, by clicking in a button from the first page. You can do it like this:

function sendData(url, data) {
  window.location = url + "#" + JSON.stringify(data);
}


function getData() {
  if (window.location.hash == "")
    return "";
  try {
    return JSON.parse(window.location.hash.substr(1));
  } catch {
    return "";
  }
}

And you can use these two functions like this:

<button onclick="sendData("page.html", [1, 2, 3])">Redirect</button>

And you can get the data by calling getData() in the second page.

You can pass the array into query string .

This question can help you: How to pass an array within a query string?

You can put the arry into query string in a.html when you go to b.html using beforeunload event, then you get this array in b.html using DOMContentLoaded event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM