• jQuery cookie操作


    一、创建一个会话cookie:

    $.cookie(‘cookieName’,'cookieValue’);

    注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie。

    二、创建一个持久cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7});

    注:当指明时间时,故称为持久cookie,并且有效时间为天。


    三、创建一个持久并带有效路径的cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/'});

    注:如果不设置有效路径,在默认情况下,只能在cookie设置当前页面读取该cookie,cookie的路径用于设置能够读取cookie的顶级目录。


    四、创建一个持久并带有效路径和域名的cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/',domain: ‘chuhoo.com’,secure: false,raw:false});

    注:domain:创建cookie所在网页所拥有的域名;secure:默认是false,如果为true,cookie的传输协议需为https;raw:默认为false,读取和写入时候自动进行编码和解码(使用encodeURIComponent编码,使用decodeURIComponent解码),关闭这个功能,请设置为true。

    五、获取cookie:

    $.cookie(‘cookieName’);   //如果存在则返回cookieValue,否则返回null。


    六、删除cookie:

    $.cookie(‘cookieName’,null);

    注:如果想删除一个带有效路径的cookie,如下:$.cookie(‘cookieName’,null,{path:’/'});

    东奔西走东奔西走

     1 jQuery.cookie = function(name, value, options) {
    2 if (typeof value != 'undefined') { // name and value given, set cookie
    3 options = options || {};
    4 if (value === null) {
    5 value = '';
    6 options.expires = -1;
    7 }
    8 var expires = '';
    9 if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
    10 var date;
    11 if (typeof options.expires == 'number') {
    12 date = new Date();
    13 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
    14 } else {
    15 date = options.expires;
    16 }
    17 expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    18 }
    19 var path = options.path ? '; path=' + options.path : '';
    20 var domain = options.domain ? '; domain=' + options.domain : '';
    21 var secure = options.secure ? '; secure' : '';
    22 document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    23 } else { // only name given, get cookie
    24 var cookieValue = null;
    25 if (document.cookie && document.cookie != '') {
    26 var cookies = document.cookie.split(';');
    27 for (var i = 0; i < cookies.length; i++) {
    28 var cookie = jQuery.trim(cookies[i]);
    29 // Does this cookie string begin with the name we want?
    30 if (cookie.substring(0, name.length + 1) == (name + '=')) {
    31 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    32 break;
    33 }
    34 }
    35 }
    36 return cookieValue;
    37 }
    38 };





  • 相关阅读:
    微信公众平台回复音乐
    Else is very important
    Generate source code from wsdl
    PHP Simple HTML DOM Parser: check elements with multiple classes
    Get Version is the first function is called?
    Remote debug GWT UI
    Remote Debug For Java Application On Tomcat
    SetStyleName引起的Regression Issue
    做乘法运算的时候需要考虑越界问题
    MySQL Uniall All
  • 原文地址:https://www.cnblogs.com/couxiaozi1983/p/2426238.html
Copyright © 2020-2023  润新知