如何封装和使用jQuery cookie的公共方法
这篇文章主要讲解了如何封装和使用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 cookie的公共方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。