如何存储持久数据客户端 [英] How to store persistent data client side

查看:182
本文介绍了如何存储持久数据客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式在客户端存储数据,而不必在每次页面加载时从服务器传输数据。我考虑生成一个动态JavaScript文件,其中包含用户当前会话所需的数据,并确保它被缓存,但这看起来非常混乱,我可以想到这样的方法有一些缺点。

I need to programmatically store data on the client side without having to transfer the data from the server on every page load. I considered generating a dynamic JavaScript file with the needed data for the current session of the user and make sure it is cached, but that seems really messy and there are a few drawbacks I can think of to such an approach.

如何在客户端存储持久数据?

How can I go about storing persistent data on the client side?

推荐答案

你可以将数据存储在 window.name 中,最多可存储2MB数据(!)。

You may store data in window.name, which can hold up to 2MB of data (!).

/* on page 1 */
window.name = "Bla bla bla";

/* on page 2 */
alert(window.name); // alerts "Bla bla bla"

编辑另外看看在这篇关于此的Ajaxian文章中。

请注意,同一标签/窗口中的其他网站也可以访问 window.name ,因此您不应在此处存储任何机密内容。

Note that other sites in the same tab/window does also have access to window.name, so you shouldn't store anything confidential here.

这篇关于如何存储持久数据客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆