- 封装jQuery的cookie文件,需要使用js文件,名称为jquery.cookie.js
;(function ($, window) {
$.extend({
addCookie: function (key, value, day, path, domain) {
let index = window.location.pathname.lastIndexOf("/")
let currentPath = window.location.pathname.slice(0, index)
path = path || currentPath
domain = domain || document.domain
if (!day) {
document.cookie = key + "=" + value + ";path=" + path + ";domain=" + domain + ";"
} else {
let date = new Date()
date.setDate(date.getDate() + day)
document.cookie = key + "=" + value + ";expires=" + date.toGMTString() + ";path=" + path + ";domain=" + domain + ";"
}
},
getCookie: function (key) {
let arr = document.cookie.split(";")
for (let i = 0; i < arr.length; i++) {
let temp = arr[i].split("=")
if (temp[0].trim() === key) {
return temp[1]
}
}
},
delCookie: function (key, path) {
addCookie(key, getCookie(key), -1, path)
}
})
})(jQuery, window);