cookie指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)
在网站客户端中保存数据,可以减少服务器的压力
// Cookie操作对象 var Cookies = {}; // cookie设置 Cookies.set = function(name, value){ var argv = arguments; var argc = arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : '/'; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); // secure值为true时,在http中是无效的;在https中才有效 }; // cookie获取 Cookies.get = function(name){ var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; var j = 0; while(i < clen){ j = i + alen; if (document.cookie.substring(i, j) == arg) return Cookies.getCookieVal(j); i = document.cookie.indexOf(" ", i) + 1; if(i == 0) break; } return null; }; // cookie删除 Cookies.del = function(name) { if(Cookies.get(name)){ var expdate = new Date(); expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); Cookies.set(name, "", expdate); } };
来自:
js中的cookie操作
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9993.html