DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Categories: Html5

Store objects in HTML5 localStorage

Share

The localStorage property allows you to store objects in HTML5 that is more secure and can store large amounts of data locally.

It’s essential sometimes having stored JavaScript object in HTML5 localStorage. But objects don’t seem to work? Let’s see.

Basically, if you have a look for Apple, Mozilla and Microsoft documentation, you can find that the functionality seems to be limited for handling the string key/value pairs.

As local storage is per origin so, all pages from one origin can store and access the same data and the storage limit is much more like 5MB. The information is never transferred to the server also.

Before HTML5, we stored application data in cookies. But now we can store data as a local storage also. This is more secure and we can store large amounts of data locally without affecting website performance.

And a workaround can be to stringify your object before storing it and then parse it when you retrieve it.

How to Store objects in HTML5?

Have a look at this example:

var obj = { '1': 'John', '2': 'Jane', '3': 'Tim' };

Now, put the object into a storage

localStorage.setItem('obj', JSON.stringify(obj));

Then, retrieve the object from a storage

var resultObj = localStorage.getItem('obj');
console.log('resultObj: ', JSON.parse(resultObj));

Read Also: Make a placeholder for a “select” box

Browser Support:

Most of the browsers supports HTML local storage.

  • Chrome >= 4.0
  • Internet Explorer/Edge >= 8.0
  • Firefoz >= 3.5
  • Safari >= 4.0
  • Opera >= 11.5
Html Developer

Recent Posts

How Did The Restaurant Industry change In Post Pandemic Era?

The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…

1 week ago

A Guide To CPQ and How It Fuels Marketing and Sales

Sales and marketing teams help attract, convert, and retain customers to ensure an organization’s long-term…

2 weeks ago

Top Questions Asked About Invoice Financing Applications

Are you an owner of a small business who’s trying to come up with ways…

3 weeks ago

Complete Medical Guide to Blood Sugar Testing and Glucose Control

Introduction When patients bring me their lab reports, the confusion is almost always the same.…

4 weeks ago

6 Tips to Strategically Remodel Your Home

Are you excited about remodeling your house after a long time? Perhaps if you're planning…

4 weeks ago

Driving Finding Balance on and off the Mat: How Calm Awareness Supports Everyday

The practice of yoga teaches us to be present, patient and mindful of our decisions.…

4 weeks ago