Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Seth A Burleson
Seth A Burleson

Posted on

     

Using LocalStorage to improve your webpage

Localstorage is a simple way to save user data on your website, by letting them save it to their browser. All it takes is a little JSON and you'll be on your way to a friendlier user experience.

Getting input

Once you've got some input from a user via Form or browsing data, You'll need to save that to the browser. Localstorage manages its storage based on a key value pair. So take the array or object your data is stored in, assign it to a variable, and stringify it with JSON. The save operation will look something like this

localStorage.setItem("keyAsAString", JSON.stringify(myDataObjOrArray));
Enter fullscreen modeExit fullscreen mode

You can decide when the localStorage should be updated, but generally, anytime the user resubmits a form or some kind of data, its a good idea to call this method again.

Retrieving values

When the user comes back to the page, check local storage for the data. Make sure to parse it using JSON or you will just get a string.

data = JSON.parse(localStorage.getItem("keyAsAString)) || []
Enter fullscreen modeExit fullscreen mode

It's very helpful to add a default if this fails, as I did in the above example.

That's all there is to localstorage. Where have you used this in a personal project?

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
sudarshansb143 profile image
sudarshan
🔥★ ɪᴛ'ꜱ тιмє ᴛᴏ вℓσω ʏᴏᴜʀ ᴍɪɴᴅ ★🔥
  • Location
    India
  • Work
    Fix Bugs and Format Code
  • Joined

Nice article.

But, be aware of consequences of using localStorage.clear() in any other app.

CollapseExpand
 
sbrevolution5 profile image
Seth A Burleson
A fullstack C# developer chronicling my journey of learning code.
  • Location
    Greensboro NC
  • Joined

I'm unfamiliar with that command, does it clear ALL localstorage? or just the localstorage for the page. As far as I know, each webpage has a seperate localstorage.

CollapseExpand
 
sudarshansb143 profile image
sudarshan
🔥★ ɪᴛ'ꜱ тιмє ᴛᴏ вℓσω ʏᴏᴜʀ ᴍɪɴᴅ ★🔥
  • Location
    India
  • Work
    Fix Bugs and Format Code
  • Joined

It will clear your whole storage regardless of how toy stored the data.

Thread Thread
 
sbrevolution5 profile image
Seth A Burleson
A fullstack C# developer chronicling my journey of learning code.
  • Location
    Greensboro NC
  • Joined

Looks like you would be better off usinglocalStorage.removeItem("keyToRemove") since it would only remove one item

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A fullstack C# developer chronicling my journey of learning code.
  • Location
    Greensboro NC
  • Joined

More fromSeth A Burleson

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp