html5 rocks 官网,HTML5 Local Storage

HTML5 Local Storage Usage In Your Web Applications

HTML5 local storage can be used in applications that need to save user data and preferences across application restarts.

In this article we would be talking about the methods using the local storage to increase the functionality of a web application.

Overview

HTML5 local storage is used for storing key value pairs on the client side. These key value pairs can be retrieved in HTML pages originating from the same domain. The Local storage data is stored on the disk and persists across application restarts

The Local Storage can be used across applications like gaming, where you save the progress of a game or a store high scores that can be later retrieved OR in Media applications, where an application embeds an audio or video stream, the app can store the timestamp of the audio/video streams in local storage.Since this data is stored across application restarts, you can start the audio/video stream from the last paused location.

JavaScript APIs

The basic methods ( JavaScript* APIs) for setting and getting item to/from storage are:

// store item

localStorage.setItem("item_key", "value");

// retrieve item

var data = localStorage.getItem("item_key");

Like other JavaScript objects, you can treat the LocalStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets.

For example, the above snippet of code can also be written as,

// store item

localStorage["item_key"]= value;

// retrieve item

var data = localStorage["item_key"];

Below is the code snippet (from Hangonman game ) which stores and retrieves the game state,

function saveGameState ()

{

localStorage["com.intel.hom.wrongGuesses"] = wrongGuesses;

localStorage["com.intel.hom.rightGuesses"] = rightGuesses;

localStorage["com.intel.hom.word"] = word;

localStorage["com.intel.hom.gameType"] = gameType;

localStorage["com.intel.hom.gameInProgress"] = gameInProgress;

}

function restoreGameState ()

{

if (localStorage && localStorage["com.intel.hom.word"] &&

localStorage["com.intel.hom.gameInProgress"] && (localStorage["com.intel.hom.gameInProgress"] === "true"))

{

wrongGuesses = localStorage["com.intel.hom.wrongGuesses"] || "";

rightGuesses = localStorage["com.intel.hom.rightGuesses"] || "";

word = localStorage["com.intel.hom.word"];

gameType = localStorage["com.intel.hom.gameType"] || 0;

gameInProgress = true;

}

else {

initGameState();

}

}

Unfortunately, present implementations only support string-to-string mappings, so you need to do marshalling to and from strings for other data structures such as an array or JavaScript object.

You can do so by using JSON.stringify() and JSON.parse() methods.

var ArrayData = [5, 6, 9];

// store array to localstorage

localStorage.setItem("list_data_key", JSON.stringify(ArrayData));

// retrieve stored data (JSON stringified) and convert

var storedData = localStorage.getItem("ArrayData ");

if (storedData) {

ArrayData = JSON.parse(storedData);

}

Here’s an example restoring the settings, from Hangonman game:

function restoreSettings ()

{

try {

useSounds = JSON.parse(localStorage["com.intel.hom.useSounds"]);

}

catch (e) {

useSounds = true;

localStorage["com.intel.hom.useSounds"] = JSON.stringify(useSounds);

}

}

Other methods that you will probably use are, removeItem and clear as below,

// For removing single Key

localStorage.removeItem("item_key"); // where 'item_key' is the key of the value you want to remove

// To clear all Local Storage

localStorage.clear();

For more information on the HTML5 storage APIs, refer to the following resources:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值