我什么时候应该使用html5 sessionStorage?

时间:2021-09-19 07:14:51

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. (more size, not traveling to server for each HTTP request like cookie does).

我可以看到localStorage可以用作更好的cookie版本。 (更大的尺寸,没有像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怎么用?

1 个解决方案

#1


20  

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.

使用ajax驱动的动态接口,很多时候没有任何东西存储接口外观的当前状态(例如,选择哪个选项卡)。 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. This would also work for actions that you only want the user to do once per login.

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

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

它是使用viewstate,隐藏字段或URL参数在页面之间传递数据的好选择。

#1


20  

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.

使用ajax驱动的动态接口,很多时候没有任何东西存储接口外观的当前状态(例如,选择哪个选项卡)。 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. This would also work for actions that you only want the user to do once per login.

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

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

它是使用viewstate,隐藏字段或URL参数在页面之间传递数据的好选择。