简体   繁体   English

可以在客户端存储信息吗?

[英]Is it possible to store information client-side?

I'm pretty new to programming so I'm unsure if this is a stupid question or not, but is it possible to store information on the client side? 我对编程很陌生,因此不确定这是否是一个愚蠢的问题,但是可以在客户端存储信息吗? Through JavaScript somehow? 通过JavaScript以某种方式?

For example say you wanted to create something basic like a to-do list or an alarm clock app. 例如,说您想创建一些基本的东西,例如待办事项列表或闹钟应用程序。 Would you need to use PHP or Rails or something to create that or is it possible to store that information on the client-side? 您是否需要使用PHP或Rails或其他东西来创建它,或者可以将这些信息存储在客户端上? If so would I use JavaScript? 如果可以,我会使用JavaScript吗?

Yep! 是的 You can use sessionStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage 您可以使用sessionStorage: https : //developer.mozilla.org/zh-CN/docs/Web/API/Window/sessionStorage

or localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage 或localStorage: https : //developer.mozilla.org/zh-CN/docs/Web/API/Window/localStorage

to store data client-side. 在客户端存储数据。 It's very easy to use too! 它也很容易使用!

是的,带有HTML5本地存储和cookie。

I you wish to store information locally you can do so at the variable localSotrage . 我希望将信息存储在本地,您可以在变量localSotrage

To write information use: 要编写信息,请使用:

localStorage.setItem(name, value);

To read information use: 要阅读信息,请使用:

localStorage.getItem(name); //Returns item value

To remove information use: 要删除信息,请使用:

localStorage.setItem(name);

Keep in mind that localStorage stores information across the whole domain . 请记住, localStorage 在整个域中存储信息。 For example, if you store the variable potato at www.mydomain.com/main , then you can retrieve it at www.mydomain.com/potato . 例如,如果将变量potato存储在www.mydomain.com/main上 ,则可以在www.mydomain.com/potato上进行检索。
I hope this helps you out. 我希望这能够帮到你。

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

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