Home > Web Front-end > Front-end Q&A > Can html5 be saved locally?

Can html5 be saved locally?

藏色散人
Release: 2023-01-28 10:25:36
Original
2123 people have browsed it

html5 can be saved locally. There are two local storage methods introduced in html5, namely: 1. localStorage, which is used to save website data for a long time. The saved data has no expiration time and can be deleted manually; 2. , sessionStorage, the data stored in sessionStorage will be deleted after the user closes the browser window.

Can html5 be saved locally?

The operating environment of this tutorial: Windows 10 system, HTML5 version, DELL G3 computer

Can html5 be saved locally?

Can.

web local storage in HTML5

##1.1.1 What is html5 web local storage (web storage)?

html5web local storage can store the user's browsing data locally. Web local storage is more secure and faster than cookies, and its data is not saved on the server. It can also store large amounts of data without affecting website performance.

html5 introduces two local storage methods: localStorage and sessionStorage.

1.1.2 Client-side storage data localStorage

is used to save website data for a long time (it will not disappear when we close the browser). The saved data has no expiration time and can be manually Delete (that is, delete through js script-delete one, delete all).

localStorage is an object, we detect the localStorage data type through typeof. The result of the detection is that its data type is object.

Commonly used APIs are as follows:

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

Read data: localStorage.getItem(key);

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

Delete all data: localStorage.clear();

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

Note: Key/value pairs---usually stored as strings

For example:

First, we declare a variable to store the username username, and assign the value to tom. This value is stored in the client's localStorage.

 <script>
        localStorage.username = &#39;张一&#39;; 
    </script>
Copy after login
Preview:

Can html5 be saved locally?## Next, we comment out localStorage.username = 'Zhang Yi';

<script>
        // localStorage.username = &#39;张一&#39;; 
        console.log(localStorage.username);
    </script>
Copy after login

Then we print localStorage. username, at this time, we will see the value of username printed out on the console.

Preview:

Can html5 be saved locally?

Save data:

localStorage.setItem(key,value);

localStorage.setItem(key:string, value:string) The data type of key is string string; the data type of value is string string.

 <script>
 localStorage.setItem(&#39;age&#39;,&#39;18&#39;)
    localStorage.setItem(&#39;sex&#39;,&#39;男&#39;)
    localStorage.setItem(&#39;tel&#39;,&#39;15856567131&#39;)
    </script>
Copy after login

Preview:

Can html5 be saved locally?Read data: localStorage.getItem(key);

 // localStorage 获取数据
    var uname=localStorage.getItem('uname')
    console.log(uname);

    var age=localStorage.getItem('age')
    console.log(age, typeof age);
   
    var tel=localStorage.getItem('tel')
    console.log(tel, typeof tel);
Copy after login

Preview:

Can html5 be saved locally?Delete single data: localStorage.removeItem(key);

<script>    
localStorage.removeItem(&#39;tel&#39;);
</script>
Copy after login

Preview:

Can html5 be saved locally? Delete all data: localStorage.clear();

<script>   
localStorage.clear();
</script>
Copy after login

Preview:

##Get the key of an index: localStorage.key(index);Can html5 be saved locally?

//在控制台中查看localStorage 是否有数据 如果length=0代表无数据
    console.log(localStorage);
//获取某个索引的key
    var k0=localStorage.key(0)
    console.log(k0);

    var k1=localStorage.key(1)
    console.log(k1);
Copy after login
Preview:

Can html5 be saved locally?

Note:

Key/value pairs---usually stored as strings

The value obtained by localStorage is a string. If we want to perform "calculation", we need to use Number() to convert the string into a number, and then participate in the calculation.

<script>           
        // 向localStorage对象中保存数据
        localStorage.setItem(&#39;num1&#39;,100)
        localStorage.setItem(&#39;num2&#39;,200)

        // 读取数据
        var num1 = localStorage.getItem(&#39;num1&#39;)
        console.log(num1, typeof num1)

        var num2 = localStorage.getItem(&#39;num2&#39;)

        var sum = Number(num1) + Number(num2);
        console.log(sum)
</script>
Copy after login
Preview:

#The content entered in the input box in the form is automatically stored in localStorage and displayed after refreshing the page. Can html5 be saved locally?

<div>
        <label>搜索</label>
    <input>
    <br>
    <h1></h1>
    </div>
Copy after login
<style>
        .box{
            width: 500px;
            margin:60px auto;

        }
    </style>
Copy after login
  <script>
          // 表单中输入框中输入的内容自动存入localStorage中,并在刷新页面后显示出来。
          //抓取元素
        var search =document.getElementById(&#39;search&#39;)
        console.log(search);
        var h1=document.getElementById(&#39;r&#39;)
        console.log(h1);
        search.onchange=function(){
        //向localStorage对象中保存数据
        localStorage.setItem(&#39;mysearch&#39;,this.value)
    }    

        window.onload=function(){
            var result=localStorage.getItem(&#39;mysearch&#39;)
            console.log(result);
            r.innerHTML=result;
               if(localStorage.length>0){
                 localStorage.removeItem(&#39;mysearch&#39;)
            }
           
    }

    </script>
Copy after login

预览:

Can html5 be saved locally?

 

1.1.3 客户端存储数据sessionStorage

sessionStorage存储的数据在用户关闭浏览器窗口后,数据会被删除。

常用的API(和localStorage的api相同)如下所示:

保存数据:sessionStorage.setItem(key,value);

读取数据:sessionStorage.getItem(key);

删除单个数据:sessionStorage.removeItem(key);

删除所有数据:sessionStorage.clear();

获取得到某个索引的key:  sessionStorage.key(index);

注意:键/值对 --- 通常以字符串存储

保存数据:sessionStorage.setItem(key,value);

  //保存数据
        sessionStorage.setItem('username','tom');
        sessionStorage.setItem('age',19);
        sessionStorage.setItem('sex','男')
        sessionStorage.setItem('tel','13866002972')

        console.log(sessionStorage);
Copy after login

 预览:

Can html5 be saved locally?

 接着,我们关闭浏览器。再次打开浏览器,打开刚刚我们访问的这个文件的地址,查看Application中sessionStorage中,看是否有数据。结果,我们发现sessionStorage中已经没有数据。如下所示:

Can html5 be saved locally?

 

由此,我们可以看到sessionStorage只是一次性保存数据。当我们关闭浏览器,或者关闭浏览器的一个窗口后,我们的数据会被删除。

读取数据:sessionStorage.getItem(key);

<script>
        var username=sessionStorage.getItem(&#39;username&#39;)
        console.log(username);
        </script>
Copy after login

 删除单个数据:sessionStorage.removeItem(key);

<script>
 sessionStorage.removeItem(&#39;age&#39;);
 </script>
Copy after login

删除所有数据:sessionStorage.clear();

<script>   
sessionStorage.clear();
</script>
Copy after login

获取得到某个索引的key:  sessionStorage.key(index);

 <script> 
  var k0=sessionStorage.key(3)
        console.log(k0);
        </script>
Copy after login

预览:

Can html5 be saved locally?

1.2 html5中MathML数学标记语言

HTML5 可以在文档中使用 MathML 元素,对应的标签是 ...

MathML 是数学标记语言,是一种基于XML(标准通用标记语言的子集)的标准,用来在互联网上书写数学符号和公式的置标语言。

 

 <div>
        <!-- sup上标标签 -->
        3<sup>3</sup>
    </div> 

    <div>
        <!-- sub下标标签 -->
        H<sub>2</sub>
    </div>
    <div>
     <math>
            <mrow>
                <msup>
                    <mi>a</mi><mn>2</mn>
                    <mo>+</mo>
                </msup>
                <msup>
                    <mi>b</mi><mn>2</mn>
                    <mo>=</mo>
                </msup>
                <msup>
                    <mi>c</mi><mn>2</mn>
                </msup>
            </mrow>
        </math>
    </div>
Copy after login

预览:

Can html5 be saved locally?

关于上标 下标,不推荐这样写。我们正常使用html中的上标sup和下标sub去写。 

推荐学习:《HTML5视频教程

The above is the detailed content of Can html5 be saved locally?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template