Web存储

191 阅读1分钟

localStorage与SessionStorage

localStorage

  • 保存在浏览器内存或硬盘
  • 永久生效,除非手动删除
  • 多窗口共享
  • 大约20M

SessionStorage

  • 保存在内存中
  • 窗口关闭时数据销毁
  • 同一个窗口下数据可以共享
  • 大约5M

都是只能存储字符串,可以将对象 JSON.stringify() 编码后存储。

API:
setItem(key,value); // 设置存储内容
getItem(key); // 根据key读取存储内容
removeItem(key); // 根据key删除内容
clear(); // 清空存储内容
key(n); // 根据索引值获取存储内容

cache manifest 缓存清单文件

eg:demo.appcache

CACHE MANIFEST

#要缓存的文件
CACHE:
    images/img1.jpg
    images/img2.jpg


#指定必须联网才能访问的文件
NETWORK:
    images/img3.jpg
    images/img4.jpg


#当前页面无法访问是回退的页面
FALLBACK:
    404.html

使用

<!DOCTYPE html>
<html manifest="demo.appcache">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<img src="images/img5.jpg" alt=""/>
···
</body>
</html>