Posted in jQuery onJune 01, 2020
本文实例讲述了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程序设计有所帮助。
jQuery cookie的公共方法封装和使用示例
- Author -
二萌偏声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@