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.
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: