简体   繁体   English

什么时候应该使用 html5 sessionStorage?

[英]When should I use html5 sessionStorage?

I've learned difference between sessionStorage (persist during session) and localStorage (persist forever if not deleted).我了解了sessionStorage (在会话期间保留)和localStorage (如果不删除则永远保留)之间的区别。

I can see that localStorage can be used as better version of cookie.我可以看到localStorage可以用作更好版本的 cookie。 (more size, not traveling to server for each HTTP request like cookie does). (更大的尺寸,不像 cookie 那样为每个 HTTP 请求前往服务器)。

But for sessionStorage , I'm thinking when should I use it effectively?但是对于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.我考虑过用户在 pageA 中的文本字段中输入,然后在同一选项卡或浏览器窗口中移至 pageB,pageB 可以查找 sessionStorage。

I can't really expand my guess more than the scenario above.除了上面的场景,我真的无法扩大我的猜测。 Could anyone tell me how can sessionStorage be used?谁能告诉我如何使用 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). 使用ajax驱动的动态接口,很多时候没有任何东西存储接口外观的当前状态(例如,选择哪个选项卡)。 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. sessionStorage可用于存储接口的状态,因此在返回页面时,您可以按用户查看的方式恢复屏幕。

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 . 另一种用法是,如果你在一个对象上工作了几页,你可以将id存储为全局变量: currentInvoiceId

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 . 你可以存储他们已经在sessionStorage看到它。 This would also work for actions that you only want the user to do once per login. 这也适用于您每次登录时只希望用户执行一次的操作。

It's a good alternative to passing data between pages using viewstate, hidden <input> fields, or URL parameters. 它是使用viewstate,隐藏<input>字段或URL参数在页面之间传递数据的好选择。

The main reason to use sessionStorage is for cases where if your user were to open the same page twice in two different tabs, you'd want separate storage areas for those two tabs. 使用sessionStorage的主要原因是,如果您的用户要在两个不同的选项卡中两次打开同一页面,那么您需要为这两个选项卡分别存储区域。 For example, consider a site where you're buying a ticket (and you can only buy one ticket, like an airline ticket flow, as opposed to a case with a shopping cart). 例如,考虑一个您购买机票的网站(您只能购买一张机票,如机票流程,而不是购物车的情况)。 If the user tries to buy two tickets in two different tabs, you wouldn't want the two sessions interfering with each other. 如果用户尝试在两个不同的选项卡中购买两张票,则您不希望两个会话相互干扰。 sessionStorage lets you track those session across multiple page loads independently. sessionStorage允许您独立地跨多个页面加载跟踪这些会话。

I view sessionStorage as simply a "tiny per-tab cache", although it can be a fairly reliable cache.我将 sessionStorage 简单地视为一个“微小的每个选项卡缓存”,尽管它可以是一个相当可靠的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM