cookie
唯一优势是它们得到了非常旧的浏览器的支持,所以如果您的项目需要支持已经过时的浏览器(比如 Internet Explorer 8 或更早的浏览器)
document.cookie = “name=”+name
web storage —— 存储简单数据(IE8)
sessionStorage
:只要浏览器开着,数据就会一直保存 (关闭浏览器时数据会丢失)
localStorage
:会一直保存数据,甚至到浏览器关闭又开启后也是这样。
localStorage.setItem('name','Chris');
localStorage.getItem('name');
localStorage.removeItem('name');
IndexedDB——存储复杂数据(IE10)
// Create an instance of a db object for us to store the open database in let db; window.onload = function() { // Open our database; it is created if it doesn't already exist // (see onupgradeneeded below) let request = window.indexedDB.open('notes', 1); // onerror handler signifies that the database didn't open successfully request.onerror = function() { console.log('Database failed to open'); }; // onsuccess handler signifies that the database opened successfully request.onsuccess = function() { console.log('Database opened successfully'); // Store the opened database object in the db variable. This is used a lot below db = request.result; // Run the displayData() function to display the notes already in the IDB displayData(); }; // Setup the database tables if this has not already been done request.onupgradeneeded = function(e) { // Grab a reference to the opened database let db = e.target.result; // Create an objectStore to store our notes in (basically like a single table) // including a auto-incrementing key let objectStore = db.createObjectStore('notes', { keyPath: 'id', autoIncrement:true }); // Define what data items the objectStore will contain objectStore.createIndex('title', 'title', { unique: false }); objectStore.createIndex('body', 'body', { unique: false }); console.log('Database setup complete'); }; };
离线文件存储——IE还不支持