![](/img/trans.png)
[英]Using Javascript, I am trying to persist the data in SessionStorage and localStorage of browser but after refreshing the page the data is lost
[英]Pass array in JavaScript to another page without using sessionStorage or localStorage
有誰知道如何在不使用sessionStorage
或localStorage
情況下將array
值傳遞給另一個頁面?
我認為你有一個頁面; 您想通過單擊第一頁上的按鈕將一些數據發送到另一頁。 你可以這樣做:
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 "";
}
}
你可以像這樣使用這兩個函數:
<button onclick="sendData("page.html", [1, 2, 3])">Redirect</button>
並且您可以通過在第二頁中調用getData()
來獲取數據。
您可以將數組傳遞給query string
。
這個問題可以幫助您: 如何在查詢字符串中傳遞數組?
當您使用beforeunload
事件轉到 b.html 時,您可以將 arry 放入 a.html 中的查詢字符串中,然后使用DOMContentLoaded
事件在 b.html 中獲取此數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.