YUI Library Home

YUI Library Examples: Storage Utility: Simple Storage Example — Saving In-Progress Text Entry

Storage Utility: Simple Storage Example — Saving In-Progress Text Entry

This example demonstrates a rudimentary use case for the Storage Utility. When you enter text in the text field below, your content will be locally saved on every fifth keypress. If your browser supports one of the Storage Utility's storage mechanisms (Gears, SWF, and HTML5 are tried, in that order), your last-saved content will persist when you reload the page.

Watch the logger on the right side of the page as you type for feedback about what the example is doing.

#

Getting Started with Storage

This example shows Storage being used in a simple context. The full source for the example follows. Of particular interest here is the instantiation of Storage, wrapped in a try/catch statement, where the catch logic handles the logic for browsers that don't support one of your requested storage mechanisms. Because we are allowing SWF storage here, we also wrap the followup logic — the logic that interacts with the fully instantiated Storage object — in the CE_READY custom event, ensuring that we are not trying to interact with the object before it's fully created. (SWFStore has a slight asynchronous delay in instantiation; that's why it's important that we listen for this event when SWF is one of our possible storage mechanisms.)

In this example, we specify Gears as our preferred storage mechanism and we provide the order in which we want Storage to progress if Gears isn't present — from Gears to SWF to HTML5.

Note that before the main code block below we have included the gears_init.js file to prepare the page for use of Gears:

Here is the main JS codeblock that drives this example, along with some inline comments to explain what's being done and why:

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

More Storage Utility Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings