在客户端(浏览器端)存储数据有诸多益处,最主要的一点是能快速访问(网页)数据。(以往)在客户端有五种数据存储方法,而目前就只有四种常用方法了(其中一种被废弃了):
Cookies
Cookies 是一种在文档内存储字符串数据最典型的方式。一般而言,cookies 会由服务端发送给客户端,客户端存储下来,然后在随后让请求中再发回给服务端。这可以用于诸如管理用户会话,追踪用户信息等事情。
此外,客户端也用使用 cookies 存储数据。因而,cookies 常被用于存储一些通用的数据,如用户的首选项设置。
Cookies 的 基本CRUD 操作
通过下面的语法,我们可以创建,读取,更新和删除 cookies:
// Create document.cookie = "user_name=Ire Aderinokun"; document.cookie = "user_age=25;max-age=31536000;secure"; // Read (All) console.log( document.cookie ); // Update document.cookie = "user_age=24;max-age=31536000;secure"; // Delete document.cookie = "user_name=Ire Aderinokun;expires=Thu, 01 Jan 1970 00:00:01 GMT"; |
Cookies 的优点
Cookies 的缺点
浏览器支持
所有主流浏览器均支持 Cookies.
Local Storage
Local Storage 是 Web Storage API 的一种类型,能在浏览器端存储键值对数据。Local Storage 因提供了更直观和安全的API来存储简单的数据,被视为替代 Cookies 的一种解决方案。
从技术上说,尽管 Local Storage 只能存储字符串,但是它也是可以存储字符串化的JSON数据。这就意味着,Local Storage 能比 Cookies 存储更复杂的数据。
Local Storage 的 基本CRUD 操作
通过下面的语法,我们可以创建,读取,更新和删除 Local Storage:
// Create const user = { name: 'Ire Aderinokun', age: 25 } localStorage.setItem('user', JSON.stringify(user)); // Read (Single) console.log( JSON.parse(localStorage.getItem('user')) ) // Update const updatedUser = { name: 'Ire Aderinokun', age: 24 } localStorage.setItem('user', JSON.stringify(updatedUser)); // Delete localStorage.removeItem('user'); |
Local Storage 的优点
相比于Cookies:
Local Storage 的缺点
浏览器支持
IE8+/Edge/Firefox 2+/Chrome/Safari 4+/Opera 11.5+(caniuse)
Session Storage
Session Storage 是 Web Storage API 的另一种类型。和 Local Storage 非常类似,区别是 Session Storage 只存储当前会话页(tab页)的数据,一旦用户关闭当前页或者浏览器,数据就自动被清除掉了。
Session Storage 的 基本CRUD 操作
通过下面的语法,我们可以创建,读取,更新和删除 Session Storage:
// Create const user = { name: 'Ire Aderinokun', age: 25 } sessionStorage.setItem('user', JSON.stringify(user)); // Read (Single) console.log( JSON.parse(sessionStorage.getItem('user')) ) // Update const updatedUser = { name: 'Ire Aderinokun', age: 24 } sessionStorage.setItem('user', JSON.stringify(updatedUser)); // Delete sessionStorage.removeItem('user'); |
优点,缺点和浏览器支持
和 Local Storage 一样
IndexedDB
IndexedDB 是一种更复杂和全面地客户端数据存储方案,它是基于 JavaScript、面向对象的和数据库的,能非常容易地存储数据和检索已经建立关键字索引的数据。
在构建渐进式Web应用一文中,我已经介绍了怎么使用 IndexedDB 来创建一个离线优先的应用。
IndexedDB 的基本 CRUD 操作
注:在示例中,我使用了 Jake's Archibald 的 IndexedDB Promised library, 它提供了 Promise 风格的IndexedDB方法
使用 IndexedDB 在浏览器端存储数据比上述其它方法更复杂。在我们能创建/读取/更新/删除任何数据之前,首先需要先打开数据库,创建我们需要的stores(类似于在数据库中创建一个表)。
function OpenIDB() { return idb.open('SampleDB', 1, function(upgradeDb) { const users = upgradeDb.createObjectStore('users', { keyPath: 'name' }); }); } |
创建或者更新store中的数据:
// 1. Open up the database OpenIDB().then((db) => { const dbStore = 'users'; // 2. Open a new read/write transaction with the store within the database const transaction = db.transaction(dbStore, 'readwrite'); const store = transaction.objectStore(dbStore); // 3. Add the data to the store store.put({ name: 'Ire Aderinokun', age: 25 }); // 4. Complete the transaction return transaction.complete; }); |
检索数据:
// 1. Open up the database OpenIDB().then((db) => { const dbStore = 'users'; // 2. Open a new read-only transaction with the store within the database const transaction = db.transaction(dbStore); const store = transaction.objectStore(dbStore); // 3. Return the data return store.get('Ire Aderinokun'); }).then((item) => { console.log(item); }) |
删除数据:
// 1. Open up the database OpenIDB().then((db) => { const dbStore = 'users'; // 2. Open a new read/write transaction with the store within the database const transaction = db.transaction(dbStore, 'readwrite'); const store = transaction.objectStore(dbStore); // 3. Delete the data corresponding to the passed key store.delete('Ire Aderinokun'); // 4. Complete the transaction return transaction.complete; }) |
如果你有兴趣了解更多关于IndexedDB的使用,可以阅读我的这篇关于怎么在渐进式Web应用(PWA)使用IndexedD。
IndexedDB 的优点
IndexedDB 的缺点
浏览器支持
IE10+/Edge12+/Firefox 4+/Chrome 11+/Safari 7.1+/Opera 15+(caniuse)
对比
Feature | Cookies | Local Storage | Session Storage | IndexedDB |
Storage Limit | ~4KB | ~5MB | ~5MB | Up to half of hard drive |
Persistent Data? | Yes | Yes | No | Yes |
Data Value Type | String | String | String | Any structured data |
Indexable ? | No | No | No | Yes |
荒野乱斗国际版 (BrawlStars)最新版本v56.274
下载一波超人内置修改器菜单版 安卓版v1.0.2
下载敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载继承了一座戏园子无限声望铜钱版 内置菜单最新版v1.7
继承了一座戏园子折相思版是游戏的破解版本,在该版本中为玩家提
山河半世橙光清软金手指版 无限鲜花v3.24
山河半世是一款超级好玩的橙光恋爱游戏,在游戏中玩家们需要扮演
蓬莱手游折相思版 安卓版v1.0.0
蓬莱免广告版是游戏的修改版本,在该版本中为玩家去除了广告,玩
当红影后橙光游戏破解版2025 最新版v1.0
当红影后橙光破解版是一款超级好玩的娱乐圈题材的橙光游戏,在这
忽然成了万人迷清软完结版 无限鲜花版v12.15
忽然成了万人迷破解版是一款非常好玩的男性向橙光游戏,在有一天