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

Web storage API overview

From: HTML5 First Look

Video: Web storage API overview

If you are building a web site and you want to keep track of who is logged in or what their favorite song is or how many widgets they've bought from you in the last month, your options are actually pretty limited. Currently you can either create a more robust server-side application, which places the burden of storing this data on the server, or you can take advantage of cookies, which allow you to store small amounts of data on a client machine. The new Web Storage API gives you a third option, and when fully implemented will make it easier to create more robust applications without placing the strain of data storage on the server.

Web storage API overview

If you are building a web site and you want to keep track of who is logged in or what their favorite song is or how many widgets they've bought from you in the last month, your options are actually pretty limited. Currently you can either create a more robust server-side application, which places the burden of storing this data on the server, or you can take advantage of cookies, which allow you to store small amounts of data on a client machine. The new Web Storage API gives you a third option, and when fully implemented will make it easier to create more robust applications without placing the strain of data storage on the server.

Before we get into the Web Storage API, let's talk a little about the current methods and their strengths and weaknesses. Cookies have been around for a long time and they are widely used to store small key value pairs on the client's machine. While extremely useful, they can only store small amounts of data, they are limited to 4 KB, and they are sent with each HTTP request. Meaning the same data is sent over and over again which can slow down your application. For a more robust solution, you can build a server-side application that uses databases or other means of persistent data storage.

Now that's great for larger applications or instances where complex databases are required, but it's certainly not always the best solution. Storing data locally is faster. It requires less load on the server and can create a more seamless user experience by eliminating the need for logging in or manually retrieving that information. So, the problem for the most part has been in finding a way to store large amounts of data on the local client that doesn't require server calls and can be written to and accessed whenever needed.

That's where the new Web Storage API comes in. Originally a part of the HTML5 specification, the Web Storage API has been split out now and can be found on its own spec on the W3C. Like cookies, the Web Storage API is designed to store data in named key/value pairs. However unlike cookies, Web Storage is designed to address two specific areas of data storage. Session storage and local storage. The session storage mechanism controls storing data for a single session in a single window.

Local storage on the other hand controls data that's accessible over multiple windows and that persists for longer than a single session. The web storage API also allows you to store considerably more data than cookies. With web storage allowing you to store an average of 5 MB based on some user agent implementation. Okay, so how do we use web storage? Well, every storage object created stores data as a list of key/value pairs. Now keys are stored as strings and although the spec states that the values can be any data type required, in reality they are stored as strings as well, so you are going to need to use conversion functions when retrieving values other than strings.

Using storage objects is actually pretty simple, you can either declare a local storage or session storage object, depending on which one you need, and then you can assign key value pairs by directly setting values as shown right here or you can use the setItem method shown here. You can also use methods to remove items, return the length of the key/value pairs in the object, specify a pair by its key index value, and one that clears the entire object of all current key/value pairs.

There is also a Storage event, which allows you to assign event listeners and respond to any changes to store data. The Storage event is fired on the window object whenever a change to the storage object occurs, and allows you to track the storage object affected, the name of the key that's been changed, the previous value of the key, the new value of the key, and the page which triggered the change. Now at the moment implementing web storage can be a bit tricky.

Although it enjoys fairly wide browser support and as you can see here it's supported by a version of almost all major browsers, now implementation can vary and trying to tie a local storage in with existing scripts can be a bit of a chore. It's also worth noting that most browsers are setting a limit of 5 MB of storage space and currently there are no mechanisms for either extending or requesting permission to increase that amount. If you want to get started with using web storage now, I recommend using Modernizr to detect web storage support and you may want to consider using jStorage.

jStorage is a small JavaScript plug-in that works with prototype, MooTools and jQuery to assist with local storage. Where available, it will go ahead and use the Web Storage API but it also provides other means for data storage for older versions of Internet Explorer or other noncompliant devices. Now what's more, the Web Storage API isn't the only emerging technology for storing local data. Google Gears included an SQLite embedded database to allow local storage and the W3C's Web SQL Database specification has now reached editor's draft status, Although it's certainly not without its own problems.

When fully implemented, Web Storage will give developers an entirely new set of tools to assist and building applications. If you are frustrated by the limitations of cookies but need something other than a server-side solution for storing and retrieving data, Web Storage is for you.

Show transcript

This video is part of

Image for HTML5 First Look
HTML5 First Look

50 video lessons · 74593 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 3m 56s
    1. Welcome
      1m 1s
    2. Using the exercise files
      1m 50s
    3. Who is this course for?
      1m 5s
  2. 21m 12s
    1. Exploring prior standards
      4m 26s
    2. Why do we need HTML5?
      3m 32s
    3. HTML5 timeline
      4m 24s
    4. Current HTML5 support
      4m 25s
    5. What HTML5 is (and what it isn't)
      4m 25s
  3. 27m 49s
    1. HTML5 vs. HTML4
      3m 25s
    2. New structural tags
      6m 1s
    3. New content tags
      4m 7s
    4. New application-focused tags
      5m 32s
    5. Deprecated elements
      4m 28s
    6. API overview
      4m 16s
  4. 22m 29s
    1. Content models
      5m 33s
    2. Understanding the outline algorithm
      3m 21s
    3. The role of ‹div› tags
      4m 20s
    4. Using ID and class attributes
      2m 6s
    5. DOCTYPE declarations
      4m 16s
    6. Character encoding
      2m 53s
  5. 41m 27s
    1. Basic page structure
      3m 40s
    2. Structuring top-level elements
      7m 30s
    3. Structuring interior content
      8m 42s
    4. Building headers
      9m 11s
    5. Checking document outlines
      5m 46s
    6. Ensuring cross-browser structure
      6m 38s
  6. 27m 53s
    1. New input types
      5m 57s
    2. Setting form autofocus
      2m 53s
    3. Using placeholder data
      4m 4s
    4. Marking required fields
      3m 24s
    5. Working with number inputs
      5m 49s
    6. Using date pickers
      5m 46s
  7. 1h 1m
    1. Canvas overview
      6m 21s
    2. Adding canvas content
      8m 58s
    3. Drawing in the canvas environment
      12m 9s
    4. Drag-and-drop API overview
      6m 18s
    5. Offline applications overview
      7m 11s
    6. Video overview
      5m 45s
    7. Encoding video
      8m 23s
    8. Adding video
      5m 58s
  8. 57m 33s
    1. Geolocation API overview
      5m 50s
    2. Web storage API overview
      5m 40s
    3. WebSockets overview
      4m 16s
    4. CSS3 overview
      6m 38s
    5. Enhancing typography with CSS3
      7m 42s
    6. Using @font-face
      7m 11s
    7. Styling HTML5 with CSS3
      10m 23s
    8. Using CSS3 transitions
      9m 53s
  9. 5m 6s
    1. Final thoughts
      3m 49s
    2. Goodbye
      1m 17s

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.

Join now Already a member? Log in

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 HTML5 First Look.

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.