要通過document.cookie屬性管理cookie,可以使用以下方法:
var cookies = document.cookie;
console.log(cookies);
document.cookie = "cookieName=cookieValue; expires=Thu, 18 Dec 2022 12:00:00 UTC; path=/";
function getCookie(name) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.indexOf(name + "=") === 0) {
return cookie.substring(name.length + 1, cookie.length);
}
}
return "";
}
console.log(getCookie("cookieName"));
function deleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
deleteCookie("cookieName");
請注意,cookie的設置需要指定過期時間(expires),路徑(path)等額外參數,以確保cookie的正確設置和管理。