标签:
web在数据在本地存储有以下三种:
document.cookie="uid=123" //设置一个值
//获取cookiefunction getCookie(name) {var cookieName = encodeURIComponent(name) + "=",cookieStart = document.cookie.indexOf(cookieName),cookieValue = null,cookieEnd;if (cookieStart > -1) {cookieEnd = document.cookie.indexOf(";", cookieStart);if (cookieEnd == -1) {cookieEnd = document.cookie.length;}cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));}return cookieValue;}//设置cookiefunction setCookie(name, value, opt_expiress, opt_path, opt_domain, opt_secure) {var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);if(opt_expiress instanceof Date){//cookie的过期时间只支持GMT格式cookieText += ";expires=" + opt_expiress.toGMTString();}if(opt_path){//path表示cookie起作用的路径,比如/path1 下设置的cookie,path2的页面则无法访问cookieText += ";path=" + opt_path;}if(opt_domain){//只能设置子域,而不能跨域cookieText += ";domain=" + opt_domain;}if(opt_secure){//安全标志,指定该标志后只有在 使用SSL连接时才会发送 cookie(即发送到https://开头的域)cookieText += ";secure";}document.cookie = cookieText;}
//删除cookiefunction unsetCookie(name,path,domain,secure) {setCookie(name, "",new Date(0),path,domain,secure);e}
<script type="text/javascript">window.onload=function () {if(window.localStorage){//检测浏览器是否支持localStorage//存储几个键值对localStorage[‘book‘] = ‘H5 开发‘;localStorage.setItem(‘author‘,‘yexd‘);localStorage.setItem(‘2016‘,‘year‘);//读取console.info(localStorage.getItem(‘book‘)); //H5 开发console.info(localStorage.author); //yexdconsole.info(localStorage[‘2016‘]); //year//删除delete localStorage[‘author‘];console.info("删除author后:" + localStorage[‘author‘]); //删除author后:undefinedlocalStorage.removeItem(‘2016‘);console.info("删除2016后:" + localStorage[‘2016‘]); //删除2016后:undefinedlocalStorage.clear();//删除所有的key}}</script>
//存储JSONvar jsonAuthor = {‘name‘:‘filod lin‘}localStorage[‘jsonAuthor‘] = jsonAuthor ;console.info(localStorage[‘jsonAuthor‘]); //[Object,Object]localStorage[‘jsonAuthor‘] = JSON.stringify(jsonAuthor);console.log(JSON.parse(localStorage[‘jsonAuthor‘]).name); //filod lin
<script type="text/javascript">window.onload=function () {if(window.localStorage){//检测浏览器是否支持localStoragewindow.addEventListener("storage",function (e) {//尚无浏览器完整实现这些事件console.log("监听Storage事件,Storage的值被改变时触发。 key=" + e.key + " ;原值 "+ e.oldValue + " ;新值=" + e.newValue +";URL=" + e.url);});}}</script>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>访问计数器</title></head><body>本次访问已经查看过该页面<span id="count1"></span>次<br/>历史上你已经查看过该页面<span id="count2"></span>次<br/><button id="btn">清零</button><script type="text/javascript">function updateCounter() {document.getElementById("count1").innerHTML = sessionStorage.pageLoadCount || 0;document.getElementById("count2").innerHTML = localStorage.pageLoadCount || 0;}//第一次进入页面时,将两个计数都设置为0if(sessionStorage.getItem("pageLoadCount") == null){sessionStorage.setItem("pageLoadCount",0);}if(localStorage.getItem("pageLoadCount") == null){localStorage.setItem("pageLoadCount",0);}//每次加载页面,把存储的数据取出后增1sessionStorage.pageLoadCount = parseInt(sessionStorage.getItem("pageLoadCount")) + 1;localStorage.pageLoadCount = parseInt(localStorage.getItem("pageLoadCount")) + 1;updateCounter();document.getElementById("btn").onclick = function () {localStorage.clear();sessionStorage.clear();updateCounter();}</script></body></html>
标签:
原文地址:http://www.cnblogs.com/yexiaodong/p/2e338b76eb231c37b8b18044f2a07719.html