赞
踩
在做项目的过程中,我们经常会对cookie进行操作。例如我们常见的存值,取值,清除指定的cookie和清除全部的cookie以及给存放的cookie设置过期时间等等。所以,根据做项目中发现的这些问题在此做个总结。
cookie中获取域名:
- /**
- * cookie中获取域名
- * */
- function GetCookieDomain() {
- var host = location.hostname;
- var ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
- if (ip.test(host) === true || host === 'localhost') return host;
- var regex = /([^]*).*/;
- var match = host.match(regex);
- if (typeof match !== "undefined" && null !== match) host = match[1];
- if (typeof host !== "undefined" && null !== host) {
- var strAry = host.split(".");
- if (strAry.length > 1) {
- host = strAry[strAry.length - 2] + "." + strAry[strAry.length - 1];
- }
- }
- return '.' + host;
- }
cookie中存值:
- /**
- * cookie中存值
- * */
- function setCookie(name, value) {
- var curWwwPath=window.document.location.href;
- var pathName=window.document.location.pathname;
- var pos=curWwwPath.indexOf(pathName);
- var localhostPath=curWwwPath.substring(0,pos); //获取地址到端口号
- var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); //项目名
- localhostPath = localhostPath.replace(/\"/g, "");
- projectName = projectName.replace(/\"/g, "");
-
- if (value) {
- var days = 1; //定义一天
- var exp = new Date();
- exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
- // 写入Cookie, toGMTString将时间转换成字符串
- // document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/video_learning" + ";domain=localhost";
- document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=" + projectName + ";domain=" + GetCookieDomain();
- }
- };
cookie中存值并设置过期时间:
- /**
- * cookie中存值并设置过期时间
- * name名字,value值,expiredays时间
- * */
- function setCookie(name, value, expiredays) {
- if (value) {
- var days = 1; //定义一天
- var exp = new Date();
- exp.setTime(exp.getTime() + expiredays);
- // 写入Cookie, toGMTString将时间转换成字符串
- document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exp.toGMTString()) + ";path=/" + ";domain=localhost";
- }
- };
cookie中取值:
- /**
- * cookie中取值
- * */
- function getCookie(name) {
- var arr,reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
- if (arr = document.cookie.match(reg)) {
- return unescape(arr[2]);
- } else {
- return null;
- }
- };
清除指定cookie值:
- /**
- * 清除指定cookie值
- * */
- function delCookie(name) {
- var exp = new Date();
- exp.setTime(exp.getTime() - 1);
- var cval = setCookie(name);
- if (cval && cval != null) {
- // document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/video_learning" + ";domain=localhost";
- document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/video_learning" + ";domain=localhost";
- }
- };
清除全部cookie值:
- /**
- * 清除全部cookie值
- * */
- function clearCookie() {
- var curWwwPath=window.document.location.href;
- var pathName=window.document.location.pathname;
- var pos=curWwwPath.indexOf(pathName);
- var localhostPath=curWwwPath.substring(0,pos); //获取地址到端口号
- var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); //项目名
- localhostPath = localhostPath.replace(/\"/g, "");
- projectName = projectName.replace(/\"/g, "");
-
- var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
- if (keys) {
- for (var i = keys.length; i--;) {
- // document.cookie = keys[i] +'=0;expires=' + new Date( 0).toUTCString() + ";path=/video_learning" + ";domain=localhost";
- // document.cookie = keys[i] +'=0;expires=' + new Date( 0).toUTCString() + ";path=" + projectName + ";domain=" + localhostPath;
- document.cookie = keys[i] +'=0;expires=' + new Date( 0).toUTCString() + ";path=" + projectName + ";domain=" + GetCookieDomain();
- }
- }
- };
cookies给网站和用户带来的好处非常多:
1、Cookie能使站点跟踪特定访问者的访问次数、最后访问时间和访问者进入站点的路径。
2、Cookie能告诉在线广告商广告被点击的次数,从而可以更精确的投放广告 。
3、Cookie有效期限未到时,Cookie能使用户在不键入密码和用户名的情况下进入曾经浏览过的一些站点。
4、Cookie能帮助站点统计用户个人资料以实现各种各样的个性化服务 。
cookies过期时间设置方式:
cookie.setMaxAge(0); //不记录cookie
cookie.setMaxAge(-1); //会话级cookie,关闭浏览器失效
cookie.setMaxAge(60*60); //过期时间为1小时
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。