客户端(浏览器端)存储数据有诸多益处,最主要的一点是能快速访问(网页)数据。目前常见的浏览器端数据存储方法有:Cookies,Local Storage,Session Storage,IndexedDB。git
Cookies 是一种在文档内存储字符串数据最典型的方式。通常而言,cookies 会由服务端发送给客户端,客户端存储下来,而后在随后让请求中再发回给服务端。这能够用于诸如管理用户会话,追踪用户信息等事情。github
此外,客户端也用使用 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 的优势cookie
Cookies 的缺点session
浏览器支持ide
全部主流浏览器均支持 Cookies.spa
Local Storage 是 Web Storage API 的一种类型,能在浏览器端存储键值对数据。Local Storage 因提供了更直观和安全的API来存储简单的数据,被视为替代 Cookies 的一种解决方案。code
从技术上说,尽管 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 是 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 是一种更复杂和全面地客户端数据存储方案,它是基于 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 的缺点
比 Web Storage API 更难于应用
浏览器支持
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 |