javascript 将数据从一个网页传递到另一个网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13509089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:59:44  来源:igfitidea点击:

Passing data from one web page to another

javascriptclient-side

提问by HammerIp

I need to pass a potentially large amount of data from one page to another using client side techniques. It is basically a list of id's which will get displayed on the target page. Obviously query string is not suitable as there could be thousands of ids so I thought I could use javascript to dynamically add a form (method=GET), write the ids into a hidden field and submit the form to the target page. It seems to work ok but I want to know if there is a better way of doing it - this feels a bit hacky.

我需要使用客户端技术将潜在的大量数据从一个页面传递到另一个页面。它基本上是一个将显示在目标页面上的 id 列表。显然查询字符串不合适,因为可能有数千个 id,所以我想我可以使用 javascript 动态添加表单(method=GET),将 id 写入隐藏字段并将表单提交到目标页面。它似乎工作正常,但我想知道是否有更好的方法 - 这感觉有点麻烦。

回答by Talha

By using html5 storage apiyou can achieve this...

通过使用html5 storage api你可以实现这个......

With HTML5, web pages can store data locally within the user's browser.

使用HTML5,网页可以在用户浏览器中本地存储数据。

Earlier, this was done with cookies. However, Web Storage is more secure and faster. The data is not included with every server request, but used ONLY when asked for. It is also possible to store large amounts of data, without affecting the website's performance.

早些时候,这是通过 cookie 完成的。但是,Web Storage 更安全、更快速。数据并不包含在每个服务器请求中,而是仅在被要求时使用。还可以在不影响网站性能的情况下存储大量数据。

The data is stored in key/value pairs, and a web page can only access data stored by itself.

数据以键/值对的形式存储,一个网页只能访问自己存储的数据。

  • localStorage- stores data with no expiration date
  • sessionStorage- stores data for one session
  • localStorage- 存储没有到期日期的数据
  • sessionStorage- 存储一个会话的数据

Example:

例子:

To set

设置

window.localStorage.setItem("name",document.getElementById("name").value);

To get

要得到

var name = window.localStorage.getItem("name");

For more reference see HTML5 storage

如需更多参考,请参阅HTML5 存储

Note:Web storage is supported in Internet Explorer 8+, Firefox, Opera, Chrome, and Safari.

注意:支持 Web 存储Internet Explorer 8+, Firefox, Opera, Chrome, and Safari

回答by Simon Edstr?m

Thusends of IDs isn't so much. If the IDs are GUIDs there will be Nx32 bytes. You could use jQuery post, which will trigger a HTTP Post.

因此,ID 的结尾并没有那么多。如果 ID 是 GUID,则将有 Nx32 个字节。您可以使用 jQuery post,它会触发 HTTP Post。