📜  javascript cookie vs 会话 vs 本地存储 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:56.696000             🧑  作者: Mango

代码示例1
// Cookies
//   Capacity: 4KB
//   Accessible: any window
//   Expiry: manually set
document.cookie = "name=Jet" + new Date(2021, 11, 31).toUTCString(); // expires Dec 31 2021
console.log(document.cookie); // result name=Jet

// Session
//   Capacity: 5MB
//   Accessible: same tab
//   Expiry: on tab close
sessionStorage.setItem("name", "Jet");
console.log(sessionStorage.getItem("name")); // result Jet
sessionStorage.removeItem("name");

// Local Storage
//   Capacity: 10MB
//   Accessible: any window
//   Expiry: never
localStorage.setItem("name", "Jet");
console.log(localStorage.getItem("name")); // result Jet
localStorage.removeItem("name");