摘要:[Javascript] Cookie 的使用方式
取得cookie值
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
設定cookie
設定cookie時要注意 " path " ,同名cookie,不同path,屬於不同的cookie
document.cookie = "CookieAccount=123132; path=/;";
刪除cookie
document.cookie = "CookieAccount=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC";
參考網站: