Horje

Tips (Total 6)


# Tips-1) What is HTML Web Storage API

HTML web storage; better than cookies.

The two mechanisms within Web Storage are as follows:

 

The Web Storage is one of the great features of HTML5. With the Web Storage feature, web applications can locally store data within the browser on the client side. It stores data in the form of key/value pair on the browser. Web Storage sometimes also known as DOM storage.

Storing data with the help of web storage is similar to cookies, but it is better and faster than cookies storage.

In compared to cookies Web Storage has Following Advantages:

Types of Web Storage

There are two types of web storage with different scope and lifetime.

 

 

Example of HTML Web Storage API

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>
 <div id="result"></div>
 <script>
  if(typeof(Storage)!=="undefined") {
   document.getElementById("result").innerHTML = "Hey, Your browser supports the Web Storage.";
  }
  else{
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage";
  }
</script>
</body>
</html>

Output should be:

Example of HTML Web Storage API

# Tips-2) What is HTML Web Storage?

With web storage, web applications can store data locally within the user's browser.

Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.



# Tips-3) What Browsers will Support HTML Web Storage API

The numbers in the table specify the first browser version that fully supports Web Storage.


# Tips-4) How to create HTML Web Storage Objects

HTML web storage provides two objects for storing data on the client:

Example of HTML Web Storage Objects

Before using web storage, check browser support for localStorage and sessionStorage:

index.html
Example: HTML
<script>
if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
} else {
  // Sorry! No Web Storage support..
}
</script>

Output should be:

Example of HTML Web Storage Objects

# Tips-5) How to create HTML localStorage Object

The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

Learn localStorage Object

<script>
// Store
localStorage.setItem("lastname", "Smith");

// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
</script>

Output should be:

Learn localStorage Object

Example explained: localStorage Object

The example above could also be written like this:

// Store
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;

Output should be:

The example above could also be written like this:

The syntax for removing the 'lastname' localStorage item is as follows:

<script>
 localStorage.removeItem("lastname");
</script>

Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!

The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:

<script>
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)."; 
</script>

Output should be:


# Tips-6) How to create HTML sessionStorage Object

The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.

Learn HTML sessionStorage Object

The following example counts the number of times a user has clicked a button, in the current session:
index.html
Example: HTML
<script>
if (sessionStorage.clickcount) {
  sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
  sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
</script>

Output should be:

Learn HTML sessionStorage Object


Share on: