/

HTML / CSS / JavaScript Tutorial

JavaScript Window property: localStorage

[this page | pdf | back links]

The localStorage property (of the JavaScript BOM window object) returns a reference to the local storage object in which it is possible to store data within a web browser (permanently) in the form of key/value pairs.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Window session storage example: (see code for local storage equivalent)<br>
(stores and then retrieves "hello" in storage container named "y", does not seem to work
with all browsers)<br>
<b id="element"></b><br>

<script>
var x = document.getElementById("element");
if (typeof(Storage) !== "undefined") {
  window.sessionStorage.setItem("y", "hello");
  x.innerHTML = window.sessionStorage.getItem("y");
  // replace sessionStorage by localStorage if data to be stored permanently
} else {
  x.innerHTML = "Your browser does not support Local / Session Storage"
}
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile