转自http://yaoqianglilan.blog.163.com/blog/static/70978316201091810435251/

本人亲测setcookie() getcookie()非常好用,其它待测试

1.js设置cookie

//set cookie   
      
function setcookie(name,value){   
    var Days = 30;   
    var exp  = new Date();   
    exp.setTime(exp.getTime() + Days*24*60*60*1000);   
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();   
}   
  
function getcookie(name){   
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));    
    if(arr != null){   
        return unescape(arr[2]);   
    }else{   
        return "";   
    }   
}   
  
function delcookie(name){   
    var exp = new Date();    
    exp.setTime(exp.getTime() - 1);   
    var cval=getCookie(name);   
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();   
}   
  
//delcookie("GD-cms");   
if (document.cookie != "") {    
    setcookie("GD-cms", "gae-django-cms");   
}   
alert(getcookie("GD-cms"));
2.jquery设置cookie 是封装到一个方法里面的 直接调用 跟php 差不多 取 设置 或者 删除

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime()   (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires='   date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path='   (options.path) : '';
        var domain = options.domain ? '; domain='   (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i  ) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length   1) == (name   '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length   1));
                    break;
                }
            }
        }
        return cookieValue;
    }

};

使用方法
jQuery操作cookie的插件,大概的使用方法如下
$.cookie('the_cookie'); //读取Cookie值
$.cookie('the_cookie', 'the_value'); //设置cookie的值
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //删除一个cookie

设置一个名称为blog,值为css9.net的cookie:

$.cookie("blog", "css9.net");

设置一个名称为blog,值为css9.net的cookie,同时设置过期时间(expires属性)为7天:

$.cookie("blog", "css9.net", { expires: 7 });

设置一个名称为blog,值为css9.net的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”

$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });

读取Cookie:

读取名称为blog的cookie值:

alert( $.cookie("blog") );

删除cookie:

$.cookie("example", null);

如果你使用了jquery插件 建议 用jquery自带的 如果 没有 就不必为了设置cookie 引入jquery了 毕竟 插件加载还是比较大的

js jquery 设置cookie的更多相关文章

  1. Jquery设置Cookie

    jQuery代码: <script src="js/jquery-1.3.1.js" type="text/javascript"></scr ...

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

    function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd ...

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

    1.引入jquery.js <script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script> ...

  4. 简易的JQuery设置Cookie

    使用之前先引用这两个文件: 然后基本的功能代码如下: <div> <input id="txtDelValues" type="text" / ...

  5. JS中设置cookie,读取cookie,删除cookie

    在开发时,碰到一个需求,需要保存一个表的信息(非隐私),希望下次打开还存在.于是想到用cookie,一番折腾完成.示例数据都是假的,打马赛克是怕泄密. 这个表取名为Data,为Array,每一行是一个 ...

  6. JS/JQuery 设置input等标签设置和取消只读属性

    <input type="text" id="HouseName" value="" align="left"/& ...

  7. jquery.cookie.js——jquery的cookie插件

    一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...

  8. <<< 网页中如何利用原生js和jquery储存cookie

    javascript当中的cookie机制,使应用达到了真正的全局变量的要求,cookie是浏览器提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由Java ...

  9. js设置cookie(原生js)

    cookie 与 session 是网页开发中常用的信息存储方式.Cookie是在客户端开辟的一块可存储用户信息的地方:Session是在服务器内存中开辟的一块存储用户信息的地方. JavaScrip ...

随机推荐

  1. js数组与字符串之间的相互转换

    一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下 <!doctype html> <html> <head> <meta charset= ...

  2. 工作中遇到的oracle分页查询问题及多表查询相关

    在工作中,有时,我们会用到oracle分页查询.这时,就需要先了解oracle的rownum.rowmun是oracle的伪列,只能用符号(<.<=.!=),而不能用这些符号(>,& ...

  3. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. C语言atoi函数(将字符串转化为int)

    头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为:int atoi (const char * str); [函数说明]atoi ...

  5. mybatis左连接需要输出左表的指定内容与筛选

    SELECT rpl.ID, rpl.DID, rpl.TRADE_TYPE, rpl.TRADE_TIME, rpl.CALL_TIME, rpl.TRADE_ADDR, rpl.RECEIVE_P ...

  6. Python学习(004)-字典{}

    特点: 无序状态 键唯一   不可变类型:字符串.整型.元组 可变类型:列表.字典   字典创建 第一种: dic1={','sex':'man'} print(dic1['name']) ----- ...

  7. 简单的C#爬虫

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  8. ubuntu16上传文件到服务器

    用windows时候,上传文件到服务器,一般都是用xshell和xftp配合使用,用ubuntu就不需要额外安装任何软件了.只用ctrl+alt+t,打开命令行用一句话就可以上传了. 将本地war包上 ...

  9. 转:Canvas标签的width和height以及style.width和style.height的区别

    转自:http://www.cnblogs.com/artwl/archive/2012/02/28/2372042.html 作者:Artwl 背景 今天在博问中看到一个问题:用canvas 的 l ...

  10. QList 列表指针的 释放

    1,使用qDeleteAll() QList<T*> list: qDeleteAll(list): list = NULL; QList<T*> *listp: qDelet ...