欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

javascript保存信息

JavaScript可以在網(wǎng)頁上存儲(chǔ)信息,可以在不同的會(huì)話中保存數(shù)據(jù)。可以在cookie中存儲(chǔ)信息,也可以在本地存儲(chǔ)中存儲(chǔ)信息,這些信息是存儲(chǔ)在本地瀏覽器中的。如果用戶在同一個(gè)網(wǎng)站上多次訪問,那么這些信息就會(huì)被帶到后續(xù)的會(huì)話中。JavaScript保存信息的方式很多,下面我們分別來看。

1. cookie中存儲(chǔ)信息

document.cookie = "username=John Doe";
//設(shè)置cookie,key = username, value = John Doe
var username = document.cookie;
//獲取cookie
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
//刪除cookie

2. localStorage和sessionStorage中存儲(chǔ)信息

localStorage.setItem("username", "John Doe");
//設(shè)置localStorage,key = username, value = John Doe
var username = localStorage.getItem("username");
//獲取localStorage
localStorage.removeItem("username");
//刪除localStorage中key為username的項(xiàng)
sessionStorage.setItem("username", "John Doe");
//設(shè)置sessionStorage,key = username, value = John Doe
var username = sessionStorage.getItem("username");
//獲取sessionStorage
sessionStorage.removeItem("username");
//刪除sessionStorage中key為username的項(xiàng)

3. IndexedDB中存儲(chǔ)信息

var request = indexedDB.open("myDB", 1);
//打開名稱為myDB的IndexedDB,如果不存在就創(chuàng)建一個(gè)版本為1的新數(shù)據(jù)庫
request.onsuccess = function(event) {
var db = event.target.result;
var transaction = db.transaction(["users"], "readwrite");
var objectStore = transaction.objectStore("users");
var request = objectStore.add({ username: "John Doe", email: "johndoe@example.com" });
//將{username: "John Doe", email: "johndoe@example.com"}存入名稱為users的對(duì)象倉(cāng)庫中
};

以上是JavaScript中常用的保存信息方式。對(duì)于以上三種存儲(chǔ)方式,localStorage和sessionStorage是建立在瀏覽器中的鍵值對(duì)模型上,而IndexedDB屬于數(shù)據(jù)庫模型。它們也有著各自的優(yōu)缺點(diǎn),開發(fā)者可以根據(jù)實(shí)際需求選擇適合自己的存儲(chǔ)方式。