欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery cookie的公共方法封装和使用示例

程序员文章站 2022-04-09 16:20:20
本文实例讲述了jquery cookie的公共方法封装和使用。分享给大家供大家参考,具体如下:common.js放入公共方法:/** * 刷新cookie */var cookiespath = '/...

本文实例讲述了jquery cookie的公共方法封装和使用。分享给大家供大家参考,具体如下:

common.js放入公共方法:

/**
 * 刷新cookie
 */
var cookiespath = '/';
var cookiestime = 3 / 24;
 
function refreshcookie() {
  var cookiedata = $.cookie();
  $.each(cookiedata, function (_key, _value) {
    $.cookie(_key, _value, {path: cookiespath, expires: cookiestime});
  });
}/**
 * 刷新cookie
 */
var cookiespath = '/';
var cookiestime = 3 / 24;
 
function refreshcookiecopy() {
  var cookiedata = _$.cookie();
  _$.each(cookiedata, function (_key, _value) {
    _$.cookie(_key, _value, {path: cookiespath, expires: cookiestime});
  });
}
 
/**
 * 设置cookie
 * @param key
 * @param value
 */
function setcookie(key, value) {
  refreshcookie();
  $.cookie(key, value, {path: cookiespath, expires: cookiestime});
}
/**
 * 设置_cookie
 * @param key
 * @param value
 */
function setcookiecopy(key, value) {
  refreshcookiecopy();
  _$.cookie(key, value, {path: cookiespath, expires: cookiestime});
}
 
/**
 * 设置cookie by time
 * @param key
 * @param value
 */
function setcookiebytime(key, value, time) {
  // refreshcookie();
  $.cookie(key, value, {path: cookiespath, expires: time});
}
 
/**
 * 获取cookie
 * @param key
 */
function getcookie(key) {
 
  return $.cookie(key);
}
 
/**
 * 删除cookie
 * @param key
 */
function deletecookie(key) {
  $.removecookie(key, {path: cookiespath});
}
 
/**
 * 清除cookie
 */
function clearcookie() {
  var cookiedata = $.cookie();
  $.each(cookiedata, function (key, value) {
    deletecookie(key);
  });
}
 
/**
 * 清除单个cookie
 */
function clearcookiesingle(name) {
  setcookie(name, "", -1);
}

如何使用:

function usecookie() {
  setcookie("name","gaopian");
  getcookie("name");
  deletecookie("name");
  clearcookie();
}