javascript 在 Web 应用程序中存储小型 UI 用户首选项的最佳方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6534535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:59:31  来源:igfitidea点击:

Best way to store small UI user preferences in web app?

javascriptjqueryweb-applicationscookieslocal-storage

提问by wilsonpage

I have a web app I'm developing and quite like the idea that if the user makes changes to the view of the UI (eg. Lists open or closed, certain viewing preferences) those changes remain after they close the browser and visit the web app at a later date.

我有一个正在开发的网络应用程序,并且非常喜欢这样的想法,即如果用户对 UI 的视图进行更改(例如,打开或关闭列表、某些查看首选项),这些更改在关闭浏览器并访问网络后仍会保留应用程序在稍后的日期。

Possible options I can think of:

我能想到的可能选项:

  1. Store UI preferences JSON object in cookie.
  2. Use HTML5 local storage (no experience of this)
  3. Store in mySQL database (not keen on storing such trivial data in the DB)
  1. 将 UI 首选项 JSON 对象存储在 cookie 中。
  2. 使用 HTML5 本地存储(没有这方面的经验)
  3. 存储在 mySQL 数据库中(不热衷于将这些琐碎的数据存储在 DB 中)

Once stored I will retrieve these preferences when the user returns and set the UI as it was when they last left it.

存储后,我将在用户返回时检索这些首选项,并将 UI 设置为他们上次离开时​​的状态。

Does anyone have any experience of this type of feature and can suggest the best method for UI state save and retrieval?

有没有人对这种类型的功能有任何经验,并且可以建议 UI 状态保存和检索的最佳方法?

采纳答案by bhagyas

If it's so small you should probably store the UI settings json object in your own database.

如果它太小,您可能应该将 UI 设置 json 对象存储在您自己的数据库中。

That will leave users with less problems at their end, like cache clearance and local storage unsupported browsers.

这将使用户最终遇到更少的问题,例如缓存清除和本地存储不受支持的浏览器。

It will also make it possible for users to use the UI settings across different computers or browsers.

它还将使用户可以跨不同的计算机或浏览器使用 UI 设置。

回答by zatatatata

Using HTML5 local storage is really easy:

使用 HTML5 本地存储非常简单:

localStorage.setItem('someName', 'savedData');

When you have to retrieve it later you just use:

当您以后必须检索它时,您只需使用:

var data = localStorage.getItem('someName');

Using HTML5 has some drawback too, I suggest you to definitely add support to older browsers via cookie etc. Then again if you wish to enable the user to have the same settings regardless of the location he logs in from, you have no other option than to use a database table.

使用 HTML5 也有一些缺点,我建议你一定要通过 cookie 等方式添加对旧浏览器的支持。如果你想让用户无论从哪个位置登录,都可以拥有相同的设置,你别无选择,只能使用数据库表。

Extra note:Beware that cookies are sent with each request to the server. If low bandwidth is important and there are numerous settings that are defined in a cookie, using a database table or local storage makes more sense.

额外注意:请注意,cookie 是随每个请求发送到服务器的。如果低带宽很重要并且 cookie 中定义了许多设置,则使用数据库表或本地存储更有意义。

回答by Nivas

Do you want the look and field changes remembered across multiple machines? Are they critical or will they cause major user frustrations if they are forgotten (clear cookies)?

您是否希望在多台机器上记住外观和字段更改?如果它们被遗忘(清除 cookie),它们是重要的还是会引起主要用户的沮丧?

The answer to this question decides whether you store locally or on the server.

这个问题的答案决定了你是在本地存储还是在服务器上。

If this is not required then a cookie or local storage should be sufficient. HTML 5 local storageis not really prevalent(a little old question).

如果这不是必需的,那么 cookie 或本地存储就足够了。HTML 5 本地存储并不是很普遍(一个老问题)。

If you want to remember across machines, a database is the only option.

如果你想跨机器记住,数据库是唯一的选择。

回答by Jordan Wallwork

I've done this kind of thing before using cookies. I'd never want to store this kind of stuff in a database, and since its not a hugely important feature caching issues or disabled cookies shouldn't really matter - some people won't benefit from it but the majority probably will. If you want the users state to persist over different computers though you'll have to do what bhagyas suggested and store it in the database.

我在使用cookies之前就做过这种事情。我永远不想将这种东西存储在数据库中,而且由于它不是一个非常重要的功能,缓存问题或禁用的 cookie 不应该真正重要 - 有些人不会从中受益,但大多数人可能会。如果您希望用户状态持续在不同的计算机上,尽管您必须按照 bhagyas 的建议进行操作并将其存储在数据库中。