Viewers: in countries Watching now:
Often, designers need to convey simple data without the overhead of a relational database such as MySQL. This course introduces web technologies that make it easy for a designer or developer to store, retrieve, and display data online.
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.
There are currently no FAQs about Working with Data on the Web.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.