zoukankan      html  css  js  c++  java
  • HTML5(七)Web 存储

    HTML5 Web 存储


    HTML5 web 存储,一个比cookie更好的本地存储方式。


    什么是 HTML5 Web 存储?

    使用HTML5可以在本地存储用户的浏览数据。

    早些时候,本地存储使用的是 cookie。但是Web 存储需要更加的安全与快速. 这些数据不会被保存在服务器上,

    但是这些数据只用于用户请求网站数据上.它也可以存储大量的数据,而不影响网站的性能。

    数据以 键/值 对存在, web网页的数据只允许该网页访问使用。

    localStorage 和 sessionStorage

    客户端存储数据的两个对象为:

    • localStorage - 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除。
    • sessionStorage - 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。

    在使用 web 存储前,应检查浏览器是否支持 localStorage 和sessionStorage:

    if(typeof(Storage)!=="undefined")
    {
        document.write("nice! 支持 localStorage  sessionStorage 对象!");
    } else {
        document.write("抱歉! 不支持 web 存储。"); 
    }
    

    两者常用API

    功能 API( 以localStorage为例,sessionStorage用法一样 )
    添加键值对 localStorage.setItem(key,value)
    通过键获取值 localStorage.getItem(key)
    删除单个数据 localStorage.removeItem(key)
    删除所有数据 localStorage.clear()
    通过下标获取属性名 localStorage.key(index)
    键值对数量 localStorage.length

    提示: 键/值对通常以字符串存储,你可以按自己的需要转换该格式。

    实例

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>web储存</title>
    	<script>
    		function count() {
    			if (sessionStorage.clickcount)
    				sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
    			else
    				sessionStorage.clickcount=1;
    			var result = document.getElementById("result");  
    			result.innerHTML = "目前点击次数:" + sessionStorage.clickcount;  
    		}
    
    		function count2() {
    			if (localStorage.clickcount)
    				localStorage.clickcount=Number(localStorage.clickcount)+1;
    			else
    				localStorage.clickcount=1;
    			var result_now = document.getElementById("result_now");  
    			result_now.innerHTML = "历史点击次数:" + localStorage.clickcount;  
    		}
    	</script>
    </head>
    
    <body>
    	<br/>
    	<button onclick="count()">计数</button>
    	<p id="result"><br/></p>
    
    	<button onclick="count2()">计数</button>
    	<p id="result_now"><br/></p>
    </body>
    </html>
    

    localStorage 对象

    localStorage 对象存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。

    实例

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>SessionStorage</title>
    	<script>
    	//保存数据  
    	function save(){  
    		var fruit = document.getElementById("fruit").value; 
    		var fruit_En = document.getElementById("fruit_En").value;   
    		localStorage.setItem(fruit, fruit_En);
    		alert("添加成功");
    	}
    	//查找数据  
    	function find(){  
    		var search = document.getElementById("search").value;  
    		var fruit = localStorage.getItem(search);  
    		var find_result = document.getElementById("find_result");  
    		find_result.innerHTML = search + "的英文是:" + fruit;  
    	}
    </script>
    </head>
    <body>
    	<div style="border: 2px dashed #ccc;320px;text-align:center;">     
    		<label for="fruit">水 果(key):</label>  
    		<input type="text" id="fruit" name="fruit" class="text"/>  
    		<br/>  
    
    		<label for="fruit_En">单 词(value):</label>  
    		<input type="text" id="fruit_En" name="fruit_En"/>  
    		<br/>  <br/>  
    		<input type="button" onclick="save()" value="新增记录"/>  
    		<br/>  <hr/>  
    		<label for="search">输入水果名:</label>  
    		<input type="text" id="search" name="search"/>  
    		<input type="button" onclick="find()" value="查找英文"/>  
    		<p id="find_result"><br/></p>  
    	</div>
    </body>
    </html>
    

    以上实例只是演示了简单的 localStorage 存储与查找,更多情况下我们存储的数据会更复杂。

    JSON.stringify 来存储对象数据,JSON.stringify 可以将对象转换为字符串。

    JSON.parse是从一个字符串中解析出json(键值对)

  • 相关阅读:
    SpringBoot项目部署与服务配置
    JDBC链接oracle已经mysql的测试
    Jedis工具类
    jsp&Sevelet基础详解
    Mysql和Oracle数据库concat()函数
    如何在maven工程中加载oracle驱动
    获取日期的相关方法
    SpringBoot配置Email发送功能
    spring中schedule注解的使用
    新建一个新的spring boot项目
  • 原文地址:https://www.cnblogs.com/1101-/p/12593467.html
Copyright © 2011-2022 走看看