HTML Web Storage API

Web hosting is more secure, and large amounts of data can be stored locally without impacting website performance.

HTML web storage is better than cookies .

 

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, which were included in every server request. Web hosting is more secure, and large amounts of data can be stored locally without impacting website performance.

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

Web hosting is origin-based (by domain and protocol). All pages from a single source can store and access the same data.

Browser support

The numbers in the table indicate the first browser version to fully support Web Storage.

API
Web Storage
Google Chrome MS Edge Firefox Safari Opera
4.0 8.0 3.5 4.0 11.5

HTML Web Storage Objects

HTML Web Storage provides two objects for storing data on the client side:

  • window.localStorage- storing data without an expiration date
  • window.sessionStorage- Stores data for a session (data is lost when closing the browser tab)

Before using web hosting, check your browser's support for localStorageand sessionStorage:

if (typeof(Storage) !== "undefined") { // Code for localStorage/sessionStorage. } else { // Sorry! No Web Storage support. }

localStorage object

This object localStoragestores data without an expiration date. The data will not be deleted when the browser is closed and will be available the following day, week, or year.

 

For example:

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

The example is explained as follows:

  • Create a name/value pair for localStorage with name="lastname" and value="Smith"
  • Get the value of "lastname" and insert it into the element with id="result"

The example above could also be written like this:

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

The syntax for deleting the localStorage entry "lastname" is as follows:

localStorage.removeItem("lastname");

Note : Name/value pairs are always stored as strings. Remember to convert them to other formats when necessary!

The following example counts the number of times a user has clicked a button. In this code, the string value is converted into a number so that the counter can be incremented:

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

sessionStorage object

This object sessionStorageis equivalent to the `` object localStorage, except that it stores data only for a single session. The data is deleted when the user closes a particular browser tab.

The following example counts the number of times a user has clicked a button in the current session:

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.";
Related posts
Other HTML articles
  • HTML Drag & Drop API

    trong html, bất kỳ phần tử nào cũng có thể được kéo và thả.
  • HTML Geolocation API

    html geolocation api được sử dụng để xác định vị trí của người dùng.
  • YouTube video in HTML

    cách dễ nhất để phát video bằng html là sử dụng youtube.
  • Inserting video in HTML5

    cách chèn video vào html như thế nào? nếu đang muốn tìm hiểu cách chèn video vào website html, mời bạn tham khảo bài viết dưới đây.
  • What is HTML? Basic HTML structure

    html là một phần không thể thiếu khi nói đến lập trình web. mức độ phổ biến của html ngày càng tăng và nó được coi là tiêu chuẩn web chính thức. vậy cụ thể html là gì? html ra đời như thế nào và cấu trúc của html ra sao? hãy cùng tìm hiểu ngay sau đây nhé.
  • How to Learn HTML Language

    html là chữ viết tắt của hyper text markup language (ngôn ngữ đánh dấu siêu văn bản), và là loại mã hay ngôn ngữ được sử dụng để tạo nên các trang web. có thể hơi nản lòng một chút nếu bạn chưa từng viết mã bao giờ trước đây, nhưng tất cả những gì bạn cần để thử làm là một chương trình biên tập văn bản đơn giản và một trình duyệt internet. bạn thậm chí có thể nhận ra vài html được sử dụng để thay đổi kiểu chữ trên các diễn đàn trực tuyến, trong các hồ sơ trực tuyến được tùy biến, hoặc trong các bài viết của tipsmake. html là một công cụ hữu ích cho bất cứ ai sử dụng internet, việc học những thứ căn bản về html có thể mất ít thời gian hơn là bạn nghĩ.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup