我什么时候应该使用 html5 sessionStorage?
我了解了 sessionStorage
(在会话期间持续存在)和 localStorage
(如果不删除则永远持续)之间的区别。
我可以看到 localStorage
可以用作 cookie 的更好版本。 (更大的大小,不像 cookie 那样为每个 HTTP 请求传输到服务器)。
但对于 sessionStorage
,我在想什么时候应该有效地使用它?
我考虑了用户在 pageA 中的文本字段中输入的内容,然后在同一选项卡或浏览器中移至 pageB窗口中,pageB可以查找sessionStorage。
除了上面的场景之外,我无法真正扩展我的猜测。谁能告诉我sessionStorage如何使用?
I've learned difference between sessionStorage
(persist during session) and localStorage
(persist forever if not deleted).
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).
But for sessionStorage
, I'm thinking when should I use it effectively?
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.
I can't really expand my guess more than the scenario above. Could anyone tell me how can sessionStorage be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 ajax 驱动的动态界面,很多时候没有任何东西可以存储界面外观的当前状态(例如选择了哪个选项卡)。
sessionStorage
可用于存储界面的状态,因此当返回页面时,您可以恢复用户查看屏幕的方式。另一个用途是,如果您正在处理单个对象的多个页面,您可以像全局变量一样存储 id:
currentInvoiceId
。每个页面所需的用户设置(例如特殊布局或模板)可以预先加载一次并放入
sessionStorage
中以便于访问。有些内容您只希望用户每次登录时看到一次,例如新闻弹出窗口。您可以将他们已经看到的信息存储在
sessionStorage
中。这也适用于您只希望用户每次登录执行一次的操作。它是使用视图状态、隐藏
字段或 URL 参数在页面之间传递数据的一个很好的替代方案。
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.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
.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.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.It's a good alternative to passing data between pages using viewstate, hidden
<input>
fields, or URL parameters.使用 sessionStorage 的主要原因是,如果您的用户要在两个不同的选项卡中打开同一页面两次,您需要为这两个选项卡提供单独的存储区域。例如,考虑一个您要购买机票的网站(您只能购买一张机票,如机票流程,而不是带有购物车的情况)。如果用户尝试在两个不同的选项卡中购买两张门票,您不希望这两个会话相互干扰。 sessionStorage 允许您独立地跨多个页面加载跟踪这些会话。
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. 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.