Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Utilizing local storage

From: Working with Data on the Web

Video: Utilizing local storage

Local storage is a terrific tool for quickly and easily adding a sense of state to standard websites without resorting to server-side coding. In this lesson, I'm going to show you the two fundamental local storage functions and also show you how to work with it during testing and development. To demonstrate the power of local storage, we'll be looking at an almost finished product for creating a cool interactive survey that I built for my lynda.com course, HTML5 projects; creating surveys.

Utilizing local storage

Local storage is a terrific tool for quickly and easily adding a sense of state to standard websites without resorting to server-side coding. In this lesson, I'm going to show you the two fundamental local storage functions and also show you how to work with it during testing and development. To demonstrate the power of local storage, we'll be looking at an almost finished product for creating a cool interactive survey that I built for my lynda.com course, HTML5 projects; creating surveys.

The last detail that we'll add is to bring local storage online. So, let's take a look at the current state of the project without local storage. So I have the survey.htm file from chapter six 0602 folder of the exercise files now open in my browser. As you can see it says it's a survey cube. What's that?, you say, intrigued site visitor. Well, let's take the survey and find out. So I'm going to scroll down a little bit so you can get the full effect. What subjects are you most interested in? I think maybe Architecture, Photography and Sculpture for me.

Alright, I'll click next. Whoa, look at that, it's a cube. What areas of study don't you see that you wish you did? Lets see my desired course would be paper making. Okay? Next. How long have you thought of yourself as an artist? Since I was a kid, no doubt. Oh, how many hours a day do you practice your art? That's a tough one, I'd say three. Okay, next up. who have you shared your work with? I'm keeping this all to myself, so far.

And then next. Now I can review my items and submit it. Now when you get to this point, you'll notice that in the review panel, I've got a couple of items displayed, the interested and shared. Those were pooled in from the form using JQuery. But there are three in the middle that are missing. Those are the values that I'll need to store using local storage. Let's go to the code so I can show you how that's done. In my code editor, I've got one of the helper file for the project called custom_survey.js open. Its from the scripts folder in this lesson's files.

Now at the bottom of the page, I'll scroll down to it, I've got three functions, each of which are called when a particular form element is updated. Now this is the perfect spot for integrating local storage to save a changed value. And as I said before, storing a local storage item is that simple, one line of code. All you need to know is where you want to store it and what do you want to store. So let's start with that first function; desired course changed.

At the end of the function, I put my cursor and add a new line, and I can use the local upper case s, storage property, dot, set item, and put my parenthesis. And as I said, I have two attributes, the first is, the name of the item that I'm storing, and I'm going to call this desired course. Follow that with a comma, and then what's going to be stored in there, that's the value which I gather in the previous lines in this function.

And that's all the code that I need. Now it's exactly the same code for the next two functions, with one slight variation. So, let me highlight that, copy it. And now I'll move down to the artist length change, paste it in. And I want to store this under a different item name. I'm going to call this one, artist length. Let's see if I can scroll down just a little bit more. Okay, let's do one more like that for the third function. And this time we'll change desired course, or item name, to hoursPractice.

Alright, let's go ahead and save this code. Well now that we've stored our values, how do we retrieve them? For that, let's go to our main page, survey.htm. Let's scroll down to, well you see line 20 here, where the script starts, and that starts to bring everything together. And this is a JQuery function here, so I'll scroll down a little bit further. And on line 36, you see the beginning of a set of three code blocks that write out the users values to the review screen.

You'll notice that currently the three main variables, the desired course, the artists length, and the hours practiced are all set to null. This is where we'll retrieve the previously stored user values. So, we'll go right in that first one on line 36 and we'll get rid of the quotes there because we're going to be calling local storage, local storage, upper case s dot get item, as opposed to set item. And we only need one parameter here, and we'll put that in single quotes, and it is the name of the item you want to get, and for the desired course, it's desired course.

Sometimes the hardest thing about working with local storage is coming up with a set of names that make sense over and over and over again. Okay again, we'll be taking this code and basically copy, pasting, altering in to other functions. So here, at the artist length, the item we want to get is artist length. One more time. Let's get rid of our double quotes there.

Paste it in and we will, of course, get hoursPractice, okay. I'm going to make sure to save this file, so I now have both of them saved. And we're ready for our test run. Back to the browser. Now I'm going to go ahead and click refresh. So, they'll start over. And let's step through the survey. Well, I'm feeling more like film, video, graphic design, still interested in sculpture, so let's do those three. Hit next, and I still like that paper-making concept.

I still thought of myself as an artist since I was a kid. And art, yeah, I'll go up to four. And I still have not shared it with anybody. Okay. And now, it looks like our local storage is working well. There's our three, previously missing user values. Bravo. So, I promised that I'd show you how to work with local storage during development and testing. You'll need to use a browser that has development tools, but all the modern ones do, so it's really no problem. In Chrome, what you do is go to view, developer, developer tools.

Once you're there, go to the resources tab and you'll see an entry for local storage. If there's anything stored in local storage, you can expand that entry and click on it, and there you'll see the items that have already previously been stored. Now, we see that three that I worked on artist length since I was a kid, desired course, paper-making, hours practised, four. Again, one thing to keep in mind, we're always working with strings when you work with local storage, so this four is actually a string.

If you ever need to retrieve it and do something mathematical with it, you'll have to convert it to an integer. Now you notice a fifth entry and, this is because of another course that I was working on, using local storage. As I said, I really like this facility. And this one is just left over, there's no value left in it. So, I'm going to go ahead and just select it, and then press the Delete key. Now you can go ahead and delete local storage as you're doing your development, you can even go in and edit the value, if you need to.

I really think local storage is a excellent technology for integrating data on the web, and I use it in many of my projects. If you want to know more about how to build a custom survey cube, like this one, check out my lynda.com course, HTML5 projects; creating surveys.

Show transcript

This video is part of

Image for Working with Data on the Web
Working with Data on the Web

25 video lessons · 11558 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Working with Data on the Web.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.