亲宝软件园·资讯

展开

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

二萌偏 人气:0

本文实例讲述了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();
}

希望本文所述对大家jQuery程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论