我什么时候应该使用html5 sessionStorage? [英] When should I use html5 sessionStorage?

查看:468
本文介绍了我什么时候应该使用html5 sessionStorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解了 sessionStorage (会话期间保持不变)和 localStorage 之间的区别(如果没有删除则永久保留) 。

I've learned difference between sessionStorage (persist during session) and localStorage (persist forever if not deleted).

我可以看到 localStorage 可以用作更好的cookie版本。 (更大的尺寸,不会像cookie那样为每个HTTP请求前往服务器)。

I can see that localStorage can be used as better version of cookie. (more size, not traveling to server for each HTTP request like cookie does).

但对于 sessionStorage ,我我想什么时候才能有效地使用它?

我考虑过将用户输入到pageA中的文本字段然后移到pageB中选项卡或浏览器窗口,pageB可以查找sessionStorage。

I thought about user inputs into text fields in pageA and then moves onto pageB within the same tab or browser window, pageB can look up sessionStorage.

我无法真正扩展我的猜测,而不是上面的场景。谁能告诉我如何使用sessionStorage?

I can't really expand my guess more than the scenario above. Could anyone tell me how can sessionStorage be used?

推荐答案

使用ajax驱动的动态接口,很多时候都没有存储界面外观的当前状态(例如,选择哪个选项卡)。 sessionStorage 可用于存储界面的状态,因此当返回页面时,您可以按照用户查看的方式恢复屏幕。

With ajax-driven dynamic interfaces, a lot of times there is nothing storing the current state of how the interface looks (like which tab is selected, for example). sessionStorage could be used to store the state of the interface, so when coming back to a page, you can restore the screen the way the user was looking at it.

另一种用法是,如果你在一个对象上工作几个页面,你可以将id存储为全局变量: currentInvoiceId

Another use would be if several pages deep you are working on a single object, you could store the id like a global variable: currentInvoiceId.

每个页面上需要的用户设置,如特殊布局或模板,可以预先加载一次并放入 sessionStorage 便于访问。

User settings that are needed on every page, like a special layout or template, could be loaded once up front and put into sessionStorage for easy access.

您只希望用户在每次登录时看到一些内容,例如新闻弹出窗口。你可以存储他们已经在 sessionStorage 中看到它。这也适用于您只希望用户每次登录一次的操作。

Some things you only want the user to see once per login, like a news popup. You could store that they've seen it already in sessionStorage. This would also work for actions that you only want the user to do once per login.

这是使用viewstate在页面之间传递数据的一个很好的替代方法,隐藏< input> 字段或网址参数。

It's a good alternative to passing data between pages using viewstate, hidden <input> fields, or URL parameters.

这篇关于我什么时候应该使用html5 sessionStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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