本地存储只能查看当前域名所属信息
- 知识点Cookie信息有效期,所属域名路径
- SessionStorage和LocalStorage可以存储任意字符内容
- Storage的存储容量比Cookie的容量更大,H5开发中很常用
- Cookie和Storage都支持查看,增加,修改,清除
- Cookie存数的数据,当过期日期小于当前日期,择自动删除
- SessionStorage存储数据在关闭浏览器之后就没了
- LocalStorage存储数据可以长期保存,没有截止有效有效期
// 写入Cookie
var addCookieBtn = document.getElementById("addCookieBtn");
addCookieBtn.addEventListener("click" , function(){
document.cookie = "imooc" + "=" + escape("test");
content.innerHTML = content.innerHTML + "<br/>" + "Cookie写入完成";
});
//==========================================================
// 写入SessionStorage(关闭浏览器,数据消失)
var addSessionStorageBtn = document.getElementById("addSessionStorageBtn");
addSessionStorageBtn.addEventListener("click" , function(){
window.sessionStorage.site = 'http://www.imooc.com';
window.sessionStorage.name = 'test';
content.innerHTML = content.innerHTML + "<br/>" + "SessionStorage写入完成";
});
//==========================================================
// 写入LocalStorage
var addLocalStorageBtn = document.getElementById("addLocalStorageBtn");
addLocalStorageBtn.addEventListener("click" , function(){
window.localStorage.site = 'http://www.imooc.com';
window.localStorage.name = 'test';
content.innerHTML = content.innerHTML + "<br/>" + "LocalStorage写入完成";
});
// 控制台输出
var consoleBtn = document.getElementById("consoleBtn");
consoleBtn.addEventListener("click" , function(){
console.warn('test');
console.debug('test');
console.info('test');
console.error('test');
content.innerHTML = content.innerHTML + "<br/>" + "控制台输出完成";
});
网友评论