关于localStorage的实际应用
在客户端存储数据
HTML5 提供了两种在客户端存储数据的新方法:
- localStorage - 没有时间限制的数据存储
- sessionStorage - 针对一个 session 的数据存储
之前,这些都是由 cookie 完成的。但是 cookie 不适合大量数据的存储,因为它们由每个对服务器的请求来传递,这使得 cookie 速度很慢而且效率也不高。
在 HTML5 中,数据不是由每个服务器请求传递的,而是只有在请求时使用数据。它使在不影响网站性能的情况下存储大量数据成为可能。
对于不同的网站,数据存储于不同的区域,并且一个网站只能访问其自身的数据。
HTML5 使用 JavaScript 来存储和访问数据。

1.html代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>搜索中心</title>
<link rel="stylesheet" href="assets/mui/css/mui.min.css">
<link rel="stylesheet" href="assets/fa/css/font-awesome.min.css">
<link rel="stylesheet" href="css/mobile.css">
</head>
<body>
<div class="lt_container">
<!--顶部-->
<header class="lt_topBar">
<a href="javascript:history.back();" class="mui-icon mui-icon-back left"></a>
搜索中心
</header>
<!--内容-->
<div class="lt_content">
<div class="lt_wrapper">
<!--搜索框-->
<div class="lt_search">
<input type="search" placeholder="搜索你喜欢的商品">
<a href="javascript:;">搜索</a>
</div>
<!--历史列表-->
<div class="lt_history">
<!--TODO-->
</div>
</div>
</div>
<!--底部-->
<footer class="lt_tabs">
<a href="index.html"><span class="fa fa-home"></span><p>首页</p></a>
<a href="category.html"><span class="fa fa-bars"></span><p>分类</p></a>
<a href="user/cart.html"><span class="fa fa-shopping-cart"></span><p>购物车</p></a>
<a href="user/index.html"><span class="fa fa-user"></span><p>个人中心</p></a>
</footer>
</div>
<script type="text/template" id="list">
<% if($data.length){ %>
<div class="tit">
<span class="name">搜索历史</span>
<a class="clear" href="javascript:;"><span class="fa fa-trash"></span>清空历史</a>
</div>
<ul>
<% for(var i = $data.length-1 ; i >=0 ; i --){ %>
<li><a href="javascript:;" data-key="<%=$data[i]%>"><%=$data[i]%></a><span data-index="<%=i%>" class="fa fa-close"></span></li>
<% } %>
</ul>
<% }else{ %>
<div class="tit">
<span class="name">没有搜索历史记录</span>
</div>
<% } %>
</script>
<script src="assets/mui/js/mui.min.js"></script>
<script src="assets/zepto/zepto.min.js"></script>
<script src="assets/art-template/template-web.js"></script>
<script src="js/search.js"></script>
</body>
</html>
2.js代码
$(function () {
$('.lt_search input').val('');
/*1. 默认渲染当前历史记录*/
var storageKey = 'leTaoSearchHistoryList';
/*1.1 需要约定当前网站存历史记录的KEY和数据类型 leTaoSearchHistoryList = '["电脑","手机"]'*/
var jsonString = localStorage.getItem(storageKey) || '[]';
var historyList = JSON.parse(jsonString);
$('.lt_history').html(template('list', historyList));
//$('.lt_history').html(template('list', {list:historyList,encodeURIComponent:encodeURIComponent}));
/*2. 点击搜索记录新的搜索历史 跳转去搜索列表页*/
/*2.1 添加之后 追加在最前面*/
/*2.2 如果遇见相同的关键字 删除之前的 追加在最前面*/
/*2.3 当记录的条数超过10条 删除之前的后面的 追加在最前面*/
$('.lt_search a').on('tap', function () {
//去除输入的内容 去了两端空格
var key = $.trim($('.lt_search input').val());
// 判断是否输入
if (!key) {
mui.toast('请输入关键字');
return false;
}
// 有关键字
/*删除相同的*/
$.each(historyList, function (i, item) {
if (item == key) {
historyList.splice(i, 1);
/*中断循环*/
return false;
}
});
/*加载最后*/
historyList.push(key);
/*超过10条删掉*/
if (historyList.length > 10) {
historyList.splice(0, historyList.length - 10);
}
/*存起来*/
localStorage.setItem(storageKey, JSON.stringify(historyList));
/*渲染 会跳走 没有必要*/
//$('.lt_history').html(template('list', historyList));
/*跳转 传数据转成URL编码*/
location.href = '/mobile/searchList.html?key='+encodeURIComponent(key);
})
/*3. 点击删除 删除当前的历史记录*/
$('.lt_history').on('tap', 'li span', function () {
var index = $(this).data('index');
console.log(index);
historyList.splice(index, 1);
/*存起来*/
localStorage.setItem(storageKey, JSON.stringify(historyList));
/*渲染 会跳走 没有必要*/
$('.lt_history').html(template('list', historyList));
}).on('tap','.clear',function () {
/*4. 点击清空 清空所有的历史记录*/
historyList = [];
localStorage.setItem(storageKey, '[]');
/*渲染 会跳走 没有必要*/
$('.lt_history').html(template('list', historyList));
}).on('tap','li a',function () {
var key = $(this).data('key');
/*跳转 传数据转成URL编码*/
location.href = '/mobile/searchList.html?key='+encodeURIComponent(key);
});
});
关于localStorage的实际应用的更多相关文章
- HTML5 localStorage本地存储
介绍 localStorage(本地存储)的使用方式.包括对存储对象的添加.修改.删除.事件触发等操作. 目录 1. 介绍 1.1 说明 1.2 特点 1.3 浏览器最小版本支持 1.4 适合场景 2 ...
- 似懂非懂的localStorage和sessionStorage
一.区别 相信很多人都见过这两个关于HTML5的新名词!HTML5种的web storage包含两种存储方式:localStorage和sessionStorage,这两种方式存储的数据不会自动发给服 ...
- 将css和js缓存到localStorage缓存,提高网页响应速度
适用于小站点,这很极致,很快速~~ /** * Created by SevenNight on 2016/9/21 0021. * 插件功能:使用localStorage缓存js和css文件,减少h ...
- cookies,sessionStorage和localStorage的区别---web前端sessionStorage和localStorage区别
sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁.因此sessionStorage不是一种持久化的本地 ...
- sessionStorage 和 localStorage 、cookie
sessionStorage 和 localStorage html5中web storage包括两种储存方式:sessionStorage 和 localStorage sessionStorage ...
- 浏览器对localstorage的支持情况以及localstorage在saas系统中的应用实践思考
首先,还是要说,任何一种新特性的引入,通常有着其特有的场景和解决的目标需求,localstorage也一样.在我们的应用场景中,主要在金融业务服务的saas系统.其中涉及很多更改频率很多的元数据的客户 ...
- 初识html5的localStorage本地存储
一.概述 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是 ...
- HTML5本地存储Localstorage
什么是localstorage 前几天在老项目中发现有对cookie的操作觉得很奇怪,咨询下来是要缓存一些信息,以避免在URL上面传递参数,但没有考虑过cookie会带来什么问题: ① cookie大 ...
- HTMl5的存储方式sessionStorage和localStorage详解
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有 ...
- localStorage使用总结
一.什么是localStorage.sessionStorage 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题 ...
随机推荐
- Git单人本地仓库操作
本地仓库是个.git隐藏文件 以下为演示Git单人本地仓库操作 1.安装git sudo apt-get install git 密码:skylark 2.查看git安装结果 git 3.创建项目 在 ...
- vscode调试html文件
1. vscode调试html文件 1.1. 使用Debugger for Chrome进行调试 1.1.1. 基于本地file配置方式调试 1.1.2. 基于服务端配置方式调试 1.1.2.1. 启 ...
- AML与PIO整合问题
要想把PIO引擎封装成AML组件,面临如下问题(逐渐补充): 1)版本不兼容 内容项 AML PIO 选型 兼容? JDK 1.7 1.8 1.8 是 SPARK 1.6.1 2.1.1 HA ...
- JSP + servlet 源码 实现文件的上传
JSP页面 upLoad.jsp _________________________________ <%@ page language="java" import=&quo ...
- Python读取 csv文件中文乱码处理
需求:按行解析读取csv文件存入关系型数据库——主要是中文字体解析:遇到的问题:直接解析出来的数据为list形式,而且编码格式为unicode;解决问题:前提了解: 中文编码的规则 —— GB2312 ...
- MAVLink功能开发,移植教程。
MAVLink功能开发 -----------------本文由"智御电子"提供,同时提供视频移植教程,以便电子爱好者交流学习.---------------- 1.MAVLink ...
- program files与program files(x86)的区别
简单来说:Program Files (x86)存放了一些32位的系统文件.它和正常的Program Files以及Windows文件夹一样,都属于系统文件夹,请勿随意改动. 64位Windows中提 ...
- day 5 名片管理系统-文件版
1.添加__name__ == '__main__' if __name__ == "__main__": #添加__name__变量 #调用主函数 main() 2.添加6功能, ...
- eclipse插件SCON的SConscript文件和头文件以及C文件包含路径
1. 本次的头文件路径\Hi2110-B657SP3-SDK\src_release_657SP3\src\lib\onenet\public,以此例子作为研究,本次开发使用eclipse,用到SCO ...
- STM32的GUI库使用
1. 实验平台使用百为的STM32F103开发板 2. 例程目录\百为stm32开发板光盘\stm32_gui_lib\Project\Embedded_GUI_Example\EWARM 3. 直接 ...