Window localStorage Property - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window localStorage

Description

The localStorage and sessionStorage properties save key/value pairs in a web browser.

The localStorage property is read-only.

Return Value

The following code shows how to create a localStorage name/value pair, then retrieve the value

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {/*from w w w  . ja  v a2s  . c o m*/
    if(typeof(Storage) !== "undefined") {
        if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)+1;
        } else {
            localStorage.clickcount = 1;
        }
        document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
</body>
</html>

Related Tutorials