Share on facebook
Share on twitter
Share on linkedin
Share on email

Web Development Special: Dart Tracker (localStorage)

Since this is my first blog post I suppose I should introduce myself.  Hello my name is Andrew DeWitt and I have recently joined the team here at InfoTrust.  I have been hired on as a developer and love everything that is Mobile Development.  I have currently taken a liking to a Javascript game engine called ImpactJS.  In my series of blog posts I show you snippets of code where I run into issues.

With all that being said lets Dive in.

While in the brainstorming process of creating my HTML5 Dart Tracking app I found myself with a problem of keeping the scores of the players even if your phone suddenly goes offline.  Cookies were one way of keeping my data consistent but with HTML 5 there is an even better solution and that is localStorage.  However while working with localStorage I found it had one unique quirk that required a bit of a workaround.  That is localStorage can only hold strings.  This was just not going to work because next to all my information that needed to be tracked was in the form of Arrays.

So after some research I found two functions that allow for arrays to be properly stored in localStorage. Below is some example code to help demonstrate the process.

// Create the Array variable

var Array=[];

// Place information into the Array

Array[0]=“Test1”;

Array[1]=“Test2”;

Array[2]=“Test3”;

// Stringify the Array for inserting into localStorage

localStorage[‘Array’]=JSON.stringify(Array);

// Parse the localStorage in order to return

// your localStorage variable into a Javascript array

var storedArray = JSON.parse(localStorage[‘Array’]);

The first function you will use will be

JSON.stringify(YourArrayVariable).  This will convert your array into a string that localStorage can handle and later be parsed.

This leads me to the second function which retrieves your variable

ArrayVariable = JSON.parse(localStorage[‘NameOfArrayString’]).  And there you have it now you can store arrays in localStorage.

 

If you have any questions, leave a comment below or email me at andrew@infotrustllc.com

Share on FacebookShare on Twitter

Submit to StumbleUpon

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Other Articles You Will Enjoy

Why Executive Teams are Resisting Google Analytics 4 (and What You Can Do to Change Their Minds)

Why Executive Teams are Resisting Google Analytics 4 (and What You Can Do to Change Their Minds)

Few can dispute that the future of analytics will be driven by machine learning and artificial intelligence. With Google’s launch of Google Analytics 4…

InfoTrust to Lead Webinar on Analytics Maturity Using Google Marketing Platform

InfoTrust to Lead Webinar on Analytics Maturity Using Google Marketing Platform

If you’re a marketer looking to sharpen your skills, or a business owner hoping to take your organization to the next level, then look…

InfoTrust Earns Looker Partner Certification

InfoTrust Earns Looker Partner Certification

CINCINNATI — The experienced digital analytics and customer data governance consulting team at InfoTrust is proud to announce that the organization achieved partner status…

What is a Data Clean Room?

What is a Data Clean Room?

“Data clean room” is a growing buzzword in marketing, analytics, and advertising industries. Although data clean rooms aren’t an entirely new concept, they haven’t…

Will I Lose Data Moving from Universal Analytics to Google Analytics 4?

Will I Lose Data Moving from Universal Analytics to Google Analytics 4?

As web analysts begin preparing for the fourth generation of Google Analytics, aptly named “Google Analytics 4,” a few common questions have cropped up…

Talk To Us

Talk To Us

Receive Book Updates

Fill out this form to receive email announcements about Crawl, Walk, Run: Advancing Analytics Maturity with Google Marketing Platform. This includes pre-sale dates, official publishing dates, and more.

Our website uses cookies and may collect user information to provide a good experience. Read our Privacy Policy here.

Leave Us A Review

Leave a review and let us know how we’re doing. Only actual clients, please.