Home Web Front-end H5 Tutorial What is web in HTML5? What are the elements in web storage?

What is web in HTML5? What are the elements in web storage?

Aug 14, 2018 am 11:25 AM
html5 web

HTML5 web storage, a better local storage method than cookies. This article introduces the meaning and element parsing of HTML5 web storage.

What is HTML5 Web Storage?

Using HTML5, the user's browsing data can be stored locally.

Earlier, local storage used cookies. But Web storage needs to be more secure and fast. These data will not be saved on the server, but these data will only be used when users request website data. It can also store large amounts of data without affecting the performance of the website.

Data exists in key/value pairs, and the data of the web page is only allowed to be accessed and used by the web page.

The two objects used to store data on the client are:

localStorage - used to save the data of the entire website for a long time, and the saved data has not expired time until manually removed.

sessionStorage - Used to temporarily save the data of the same window (or tab). The data will be deleted after closing the window or tab.

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

if(typeof(Storage)!=="undefined")
{
    // 是的! 支持 localStorage  sessionStorage 对象!
    // 一些代码.....
} else {
    // 抱歉! 不支持 web 存储。
}localStorage 对象
Copy after login

There is no time limit for the data stored by the localStorage object. The data is still available after the next day, week or year.

localStorage.sitename="PHP中文网教程";
document.getElementById("result").innerHTML="网站名:" + localStorage.sitename;
Copy after login

Example analysis:

Use key="sitename" and value="Rookie Tutorial" to create a localStorage key/value pair.

Retrieve the value with key value "sitename" and insert the data into the element with id="result".

The above example can also be written like this:

// 存储
localStorage.sitename = "PHP中文网教程";
// 查找
document.getElementById("result").innerHTML = localStorage.sitename;
Copy after login

Remove ""sitename" in localStorage:

localStorage.removeItem(""sitename");
Copy after login

Whether it is localStorage or sessionStorage, the APIs that can be used are the same , the following are commonly used (take localStorage as an example):

  • Save data: localStorage.setItem(key, value);

  • Read data: localStorage.getItem(key);

  • Delete single data: localStorage.removeItem(key);

  • Delete all data: localStorage.clear();

  • Get the key of an index: localStorage.key(index);

Tips: key/value pair Usually stored as a string, you can convert this format according to your needs.

The following example shows the number of times the user 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=" 你已经点击了按钮 " + localStorage.clickcount + " 次 ";
Copy after login

sessionStorage object

The sessionStorage method stores data for a session. When the user closes the browser window, the data will be 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="在这个会话中你已经点击了该按钮 " + sessionStorage.clickcount + " 次 ";
Copy after login

The above is the introduction to the web in HTML5 and the analysis of related elements.

[Related recommendations]

What is an HTML file? A preliminary understanding of HTML files

What is HTML5? What are the characteristics, advantages and disadvantages of HTML5?

The above is the detailed content of What is web in HTML5? What are the elements in web storage?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles