百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

html5之Localstorage本地存储详解

zhezhongyun 2025-01-12 20:16 40 浏览


Localstorage本地存储基本用法

如何创建和访问 localStorage

<script type="text/javascript">localStorage.lastname="Smith";document.write(localStorage.lastname);</script>

上面就创建了一个localStorage。

我们再来做一个刷新页面,增加访问次数的功能!很简单,代码如下:

<script type="text/javascript">if (localStorage.pagecount)

?{

?localStorage.pagecount=Number(localStorage.pagecount) +1;

?}else

?{

?localStorage.pagecount=1;

?}document.write("Visits "+ localStorage.pagecount + " time(s).");</script>

上面是用点的方式进行操作,localStorage还有存储API,具体如下:

  • 调用setItem()方法,将对应的名字和值传递出去,可以实现数据存储

  • 调用getItem()方法,将名字传递出去,可以获取对应的值

  • 调用removeItem()方法,名称作为参数,可以删除对应的数据

  • 调用clear()方法,可以删除所有存储的数据

  • 使用length属性以及key()方法,传入0~length-1的数字,可以枚举所有存储数据的名字

用法如下:

localStorage.setItem("name", "haorooms"); ? ?// 以"name"为名字存储一个字符串localStorage.getItem("name"); ? ? ? ? ? ?// 获取"name"的值// 枚举所有存储的名字/值对for(var i=0; i<localStorage.length; i++){ ? ? ? ?// length表示所有的名值对总数

? ?var name = localStorage.key(i); ? ? ? ? ? ?// 获取第i对的名字

? ?var value = localStorage.getItem(name); ? ? ? ?// 获取该对的值}localStorage.removeItem("name"); ? ? ? ?// 删除"name"项localAStorage.clear(); ? ? ? ? ? ? ? ?// 全部删除

sessionStorage和Localstorage区别

sessionStorage用法和Localstorage用法一样,区别就是,sessionStorage是会话存储,关闭浏览器,存储内容就会被清除。

低版本浏览器存储兼容方案

IE User Data

微软在IE5及之后的IE浏览器中实现了它专属的客户端存储机制——“userData”。

userData可以实现一定量的字符串数据存储,对于IE8以前的IE浏览器中,可以将其用作是Web存储的替代方案。

iLocalStorage插件

由于IE8以下浏览器的本地存储API不一样,所以就写了个插件兼容各大浏览器存储。提供的方法及用法如下:

getItem: 获取属性setItem: 设置属性removeItem: 删除属性iLocalStorage.setItem('key', 'value');console.log(iLocalStorage.getItem('key'));iLocalStorage.removeItem('key');

插件代码

/**

* LocalStorage 本地存储兼容函数

* getItem: 获取属性

* setItem: 设置属性

* removeItem: 删除属性

*

*

* @example

*

? ?iLocalStorage.setItem('key', 'value');

? ?console.log(iLocalStorage.getItem('key'));

? ?iLocalStorage.removeItem('key');

*

*/(function(window, document){

? ?// 1. IE7下的UserData对象

? ?var UserData = {

? ? ? ?userData: null,

? ? ? ?name: location.href,

? ? ? ?init: function(){

? ? ? ? ? ?// IE7下的初始化

? ? ? ? ? ?if(!UserData.userData){

? ? ? ? ? ? ? ?try{

? ? ? ? ? ? ? ? ? ?UserData.userData = document.createElement("INPUT");

? ? ? ? ? ? ? ? ? ?UserData.userData.type = "hidden";

? ? ? ? ? ? ? ? ? ?UserData.userData.style.display = "none";

? ? ? ? ? ? ? ? ? ?UserData.userData.addBehavior("#default#userData");

? ? ? ? ? ? ? ? ? ?document.body.appendChild(UserData.userData);

? ? ? ? ? ? ? ? ? ?var expires = new Date();

? ? ? ? ? ? ? ? ? ?expires.setDate(expires.getDate() + 365);

? ? ? ? ? ? ? ? ? ?UserData.userData.expires = expires.toUTCString();

? ? ? ? ? ? ? ?} catch(e){

? ? ? ? ? ? ? ? ? ?return false;

? ? ? ? ? ? ? ?}

? ? ? ? ? ?}

? ? ? ? ? ?return true;

? ? ? ?},

? ? ? ?setItem: function(key, value){

? ? ? ? ? ?if(UserData.init()){

? ? ? ? ? ? ? ?UserData.userData.load(UserData.name);

? ? ? ? ? ? ? ?UserData.userData.setAttribute(key, value);

? ? ? ? ? ? ? ?UserData.userData.save(UserData.name);

? ? ? ? ? ?}

? ? ? ?},

? ? ? ?getItem: function(key){

? ? ? ? ? ?if(UserData.init()){

? ? ? ? ? ? ? ?UserData.userData.load(UserData.name);

? ? ? ? ? ? ? ?return UserData.userData.getAttribute(key);

? ? ? ? ? ?}

? ? ? ?},

? ? ? ?removeItem: function(key){

? ? ? ? ? ?if(UserData.init()){

? ? ? ? ? ? ? ?UserData.userData.load(UserData.name);

? ? ? ? ? ? ? ?UserData.userData.removeAttribute(key);

? ? ? ? ? ? ? ?UserData.userData.save(UserData.name);

? ? ? ? ? ?}

? ? ? ?}

? ?};

? ?// 2. 兼容只支持globalStorage的浏览器

? ?// 使用: var storage = getLocalStorage();

? ?function getLocalStorage(){

? ? ? ?if(typeof localStorage == "object"){

? ? ? ? ? ?return localStorage;

? ? ? ?} else if(typeof globalStorage == "object"){

? ? ? ? ? ?return globalStorage[location.href];

? ? ? ?} else if(typeof userData == "object"){

? ? ? ? ? ?return globalStorage[location.href];

? ? ? ?} else{

? ? ? ? ? ?throw new Error("不支持本地存储");

? ? ? ?}

? ?}

? ?var storage = getLocalStorage();

? ?function iLocalStorage(){

? ?}

? ?// 高级浏览器的LocalStorage对象

? ?iLocalStorage.prototype = {

? ? ? ?setItem: function(key, value){

? ? ? ? ? ?if(!window.localStorage){

? ? ? ? ? ? ? ?UserData.setItem(key, value);

? ? ? ? ? ?}else{

? ? ? ? ? ? ? ?storage.setItem(key, value);

? ? ? ? ? ?}

? ? ? ?},

? ? ? ?getItem: function(key){

? ? ? ? ? ?if(!window.localStorage){

? ? ? ? ? ? ? ?return UserData.getItem(key);

? ? ? ? ? ?}else{

? ? ? ? ? ? ? ?return storage.getItem(key);

? ? ? ? ? ?}

? ? ? ?},

? ? ? ?removeItem: function(key){

? ? ? ? ? ?if(!window.localStorage){

? ? ? ? ? ? ? ?UserData.removeItem(key);

? ? ? ? ? ?}else{

? ? ? ? ? ? ? ?storage.removeItem(key);

? ? ? ? ? ?}

? ? ? ?}

? ?}

? ?if (typeof module == 'object') {

? ? ? ?module.exports = new iLocalStorage();

? ?}else {

? ? ? ?window.iLocalStorage = new iLocalStorage();

? ?}})(window, document);

其他插件

localForage:https://github.com/mozilla/localForage

store.js:https://github.com/marcuswestin/store.js

------------------

切图网(qietu.com)是一家专门从事html5前端开发的服务机构,长期致力于提供高品质的psd转html5、响应式切图、webapp切图,h5切图等web前端开发服务。

专注web前端开发技术,关注用户体验,加我们公众微信账号:qietuwang(长按复制)

相关推荐

perl基础——循环控制_principle循环

在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...

CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅

CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...

CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士

CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...

CHAPTER 3 The Audience 第三章 接见

CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...

别搞印象流!数据说明谁才是外线防守第一人!

来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...

V-Day commemorations prove anti-China claims hollow

People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...

EasyPoi使用_easypoi api

EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...

关于Oracle数据库12c 新特性总结_oracle数据库12514

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

【开发者成长】JAVA 线上故障排查完整套路!

线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...

使用 Python 向多个地址发送电子邮件

在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

linux常用系统命令_linux操作系统常用命令

系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...

小白入门必知必会-PostgreSQL-15.2源码编译安装

一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...

Linux操作系统之常用命令_linux系统常用命令详解

Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...

linux网络命名空间简介_linux 网络相关命令

此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...