设置cookie和获取cookie

/**
 * Created by admin on 2016/3/30.
 */

function setCookie(name,value,dayToLive){
    var cookie = name + "=" +encodeURIComponent(value);
    if(typeof dayToLive === "number"){
        cookie += ";max-age="+(dayToLive * 24 * 60 * 60);

}
    document.cookie = cookie;
}

function getCookies(){
    var cookies = {};
    var all = document.cookie;
    if(all === ""){
        return cookies;
    }

var list = all.split("; ");
    for(var i=0,len=list.length;i<len;i++){
        var cookie = list[i];
        var p = cookie.indexOf("=");
        var name = cookie.substring(0,p);
        var value = decodeURIComponent(cookie.substring(p+1));
        cookies[name] = value;
    }
    return cookies;

}

setCookie("aaa","111",5);
setCookie("bbb","222",5);

var cookies = getCookies();
for(var i in cookies){

//注意!!!火狐浏览器下会默认多出两个私有属性,但均是以"_"开头
    if(cookies.hasOwnProperty(i) && i.indexOf("_") != 0){
//        document.cookie = i+"=0;expires="+new Date(0).toUTCString();
        alert(i+" : " + cookies[i]);
    }
}

//当单独针对IE写缓存时,可以使用IE userData,IE userData的作用域限制在和当前文档同目录的文档中

时间: 2024-10-07 07:56:43

设置cookie和获取cookie的相关文章

设置cookie、获取cookie、删除cookie函数封装

//设置cookie函数function setCookie(key,value,t){   var oDate = new Date();   oDate.setDate( oDate.getDate() + t );   document.cookie = key + '=' + encodeURI(value) + ';expires=' + oDate.toGMTString();} //获取cookie函数function getCookie(key){ //通过分号空格拆分开所有co

设置cookie,获取cookie,删除cookie

怎么设置cookie,怎么设置cookie以及删除cookie和cookie详解 设置cookie: 1 /** 2 * 设置cookie 3 * @param name cookie的名称 4 * @param value cookie的值 5 * @param day cookie的过期时间 6 */ 7 var setCookie = function (name, value, day) { 8 if(day !== 0){ //当设置的时间等于0时,不设置expires属性,cooki

jquery 设置cookie、删除cookie、获取cookie

1.引入jquery.js <script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script> 2.引入jquery cookie插件 <script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> 3.调用插件封装好的方法 a)设置新的cookie: $

jquery cookie用法(获取cookie值,删除cookie)

cookie在jquery中有指定的cookie操作类,下面我先来介绍我们在使用cookie操作类时的一些问题,然后介绍正确的使用方法. 使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现cookie有四个不同的属性: 名称,内容,域,路径  代码如下 复制代码 $.cookie('the_cookie'); // 读取 cookie $.cookie('the_cookie', 'the_value'); // 存储 cookie $.cookie('the_cookie'

通过js来设置cookie和读取cookie,实现登陆时记住密码的功能

function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd = $("#login_password").val(); //获取登陆密码信息 var checked = $("[name='checkbox']:checked");//获取“是否记住密码”复选框 if(checked && checked.le

封装cookie的获取,设置与查找

//获取cookiefunction getCookie(key,value){ var c = document.cookie; var str = key + '=' + value; var re = new RegExp('(^|\\s)'+str+';?($|\\s)'); return re.test(c);}//设置cookiefunction setCookie(key,val,time){ time = time?time:0; var data = new Date(); d

【学习笔记】JavaScript设置与获取Cookie

获取Cookie: 1 function getCookie(cookiename) { 2 var result; 3 var mycookie = document.cookie; 4 var start2 = mycookie.indexOf(cookiename + "="); 5 if (start2 > -1) { 6 start = mycookie.indexOf("=", start2) + 1; 7 var end = mycookie.i

javascript设置和获取cookie值

//设置cookie function setCookie(name,value,expires,path,domain,secure){ var cookieName = encodeURIComponent(name) + '=' + encodeURIComponent(value); if(expires instanceof Date){ cookieName += ';expires=' + expires; } if(path){ cookieName += ';path=' +

php设置和获取cookie

setcookie('mycookie','value');//函数原型:int setcookie(string name,string value,int expire,string path,string domain,int secure)echo($mycookie);echo($HTTP_COOKIE_VARS['mycookie']);echo($_COOKIE['mycookie']); 删除Cookie(1)调用只带有name参数的setcookie();(2)使失效时间为ti