Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 Web Storage

HTML5 Web Storage


HTML5 web storage, a better way of local storage than cookies.


What is HTML5 Web Storage?

Using HTML5, users' browsing data can be stored locally.

Earlier, local storage used cookies. But web storage is more secure and faster. These data are not saved on the server, but are only used when the user requests website data. It can also store large amounts of data without affecting website performance.

Data is stored as key/value pairs, and web page data can only be accessed by that page.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 8+, Firefox, Opera, Chrome, and Safari support web storage.

Note: Internet Explorer 7 and earlier IE versions do not support web storage.


localStorage and sessionStorage 

The two objects for client-side data storage are:

Before using web storage, you should check whether the browser supports localStorage and sessionStorage:

if

(

typeof

(

Storage

)

!==

"

undefined

"

)

{

//

Yes! localStorage and sessionStorage objects are supported!

//

Some code.....

}

else

{

//

Sorry! Web storage is not supported.

}


The localStorage Object

Data stored in the localStorage object has no time limit. It remains available the next day, the next week, or the next year.

Example

//

Storage

localStorage

.

setItem

(

"

sitename

"

,

"

Runoob Tutorial

"

)

;

//

Find

document

.

getElementById

(

"

result

"

)

.

innerHTML

=

"

Website name:

"

localStorage

.

getItem

(

"

sitename

"

)

;

Try it »

Example analysis:

The above example can also be written like this:

//

Storage

localStorage

.

sitename

=

"

Runoob Tutorial

"

;

//

Find

document

.

getElementById

(

"

result

"

)

.

innerHTML

=

localStorage

.

sitename

;

Remove "sitename" from localStorage:

localStorage

.

removeItem

(

"

sitename

"

)

;

Whether localStorage or sessionStorage, the available APIs are the same; the commonly used ones are as follows (taking localStorage as an example):

Tip: Key/value pairs are usually stored as strings, and you can convert the format as needed.

The following example shows how many times the user has clicked the button.

The string value in the code is converted to a numeric type:

Example

if

(

localStorage

.

clickcount

)

{

localStorage

.

clickcount

=

Number

(

localStorage

.

clickcount

)

1

;

}

else

{

localStorage

.

clickcount

=

1

;

}

document

.

getElementById

(

"

result

"

)

.

innerHTML

=

"

You have clicked the button

"

localStorage

.

clickcount

"

times

"

;

Try it »


The sessionStorage Object

The sessionStorage method stores data for a session. When the user closes the browser window, the data is deleted.

How to create and access a sessionStorage:

Example

if

(

sessionStorage

.

clickcount

)

{

sessionStorage

.

clickcount

=

Number

(

sessionStorage

.

clickcount

)

1

;

}

else

{

sessionStorage

.

clickcount

=

1

;

}

document

.

getElementById

(

"

result

"

)

.

innerHTML

=

"

You have clicked this button in this session

"

sessionStorage

.

clickcount

"

times

"

;

Try it »


Developing a Simple Website List Program with Web Storage

The website list program implements the following functions:

The following code is used to save and find data:

The save() and find() Methods

//

Save data

function

save

(

)

{

var

siteurl

=

document

.

getElementById

(

"

siteurl

"

)

.

value

;

var

sitename

=

document

.

getElementById

(

"

sitename

"

)

.

value

;

localStorage

.

setItem

(

sitename

,

siteurl

)

;

alert

(

"

Added successfully

"

)

;

}

//

Find Data

function

find

(

)

{

var

search_site

=

document

.

getElementById

(

"

search_site

"

)

.

value

;

var

sitename

=

localStorage

.

getItem

(

search_site

)

;

var

find_result

=

document

.

getElementById

(

"

find_result

"

)

;

find_result

.

innerHTML

=

search_site

"

's URL is:

"

sitename

;

}

The complete example is shown below:

Example

<div style=">

border: 2px dashed #ccc;width:320px;text-align:center;

"

<label for="sitename">

Website name (key):

label

<input type="text" id="sitename" name="sitename" class="text" />
<br/>
<label for="siteurl">

URL (value):

label

<input type="text" id="siteurl" name="siteurl" />
<br/>
<input type="button" onclick="save()" value=">

Add Record

"

/>

<hr/>
<label for="search_site">

Enter website name:

label

<input type="text" id="search_site" name="search_site" />
<input type="button" onclick="find()" value=">

Find Website

"

/>

<p id="find_result">
<br/>p div>

Try it »

Screenshot of the result:

The above example only demonstrates simple localStorage storage and lookup; in most cases the data we store is more complex.

Next we will use JSON.stringify to store object data,JSON.stringify can convert objects to strings.

var

site

=

new

Object

; ...

var

str

=

JSON

.

stringify

(

site

)

;

//

Converting Objects to Strings

Then we use the JSON.parse method to convert the string to a JSON object:

var

site

=

JSON

.

parse

(

str

)

;

JavaScript implementation code:

The save() and find() Methods

//

Save data

function

save

(

)

{

var

site

=

new

Object

;

site

.

keyname

=

document

.

getElementById

(

"

keyname

"

)

.

value

;

site

.

sitename

=

document

.

getElementById

(

"

sitename

"

)

.

value

;

site

.

siteurl

=

document

.

getElementById

(

"

siteurl

"

)

.

value

;

var

str

=

JSON

.

stringify

(

site

)

;

//

Converting Objects to Strings

localStorage

.

setItem

(

site

.

keyname

,

str

)

;

alert

(

"

Saved successfully

"

)

;

}

//

Find Data

function

find

(

)

{

var

search_site

=

document

.

getElementById

(

"

search_site

"

)

.

value

;

var

str

=

localStorage

.

getItem

(

search_site

)

;

var

find_result

=

document

.

getElementById

(

"

find_result

"

)

;

var

site

=

JSON

.

parse

(

str

)

;

find_result

.

innerHTML

=

search_site

"

's website name is:

"

site

.

sitename

"

, and the URL is:

"

site

.

siteurl

;

}

The complete example is as follows:

Example

<div style=">

border: 2px dashed #ccc;width:320px;text-align:center;

"

<label for="keyname">

Alias (key):

label

<input type="text" id="keyname" name="keyname" class="text" />
<br/>
<label for="sitename">

Website name:

label

<input type="text" id="sitename" name="sitename" class="text" />
<br/>
<label for="siteurl">

URL:

label

<input type="text" id="siteurl" name="siteurl" />
<br/>
<input type="button" onclick="save()" value=">

Add Record

"

/>

<hr/>
<label for="search_site">

Enter alias (key):

label

<input type="text" id="search_site" name="search_site" />
<input type="button" onclick="find()" value=">

Find Website

"

/>

<p id="find_result">
<br/>p div>

Try it »

The loadAll in the example outputs all stored data; you need to ensure that the data stored in localStorage is in JSON format, otherwise JSON.parse(str) will throw an error.

Output result demonstration: