简体   繁体   English

不使用 sessionStorage 或 localStorage 将 JavaScript 中的数组传递到另一个页面

[英]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.并且您可以通过在第二页中调用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.

相关问题 使用 Javascript,我试图将数据保存在浏览器的 SessionStorage 和 localStorage 中,但刷新页面后数据丢失 - Using Javascript, I am trying to persist the data in SessionStorage and localStorage of browser but after refreshing the page the data is lost Javascript:是否只有一页的 sessionStorage 或 localStorage 之类的东西? - Javascript: Is there something like sessionStorage or localStorage for one page only? 将javascript数组传递给另一个页面 - Pass javascript array to another page 在不使用cookie的情况下将变量的值传递到另一个页面(Javascript) - Pass a variable's value to another page without using cookies ( Javascript ) 使用 localStorage/sessionStorage 存储数组 - Using localStorage/sessionStorage to store arrays 使用 localStorage / sessionStorage 的性能问题? - Performance issues with using localStorage / sessionStorage? 使用localStorage和sessionStorage作为元素树 - Using localStorage and sessionStorage as tree of elements SessionStorage和重新加载页面的javascript - SessionStorage and reloading page javascript 如何在javascript中清除localstorage,sessionStorage和cookies?然后检索? - how to clear localstorage,sessionStorage and cookies in javascript? and then retrieve? 将多维javascript数组传递给另一个页面 - pass multidimensional javascript array to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM