[英]Pass array in JavaScript to another page without using sessionStorage or localStorage
有谁知道如何在不使用sessionStorage
或localStorage
情况下将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.并且您可以通过在第二页中调用
getData()
来获取数据。
You can pass the array into query string
.您可以将数组传递给
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.当您使用
beforeunload
事件转到 b.html 时,您可以将 arry 放入 a.html 中的查询字符串中,然后使用DOMContentLoaded
事件在 b.html 中获取此数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.