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

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

10 hours ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

1 day ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

3 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

3 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

5 days ago