cookie 是存儲(chǔ)于訪問(wèn)者的計(jì)算機(jī)中的變量。每當(dāng)同一臺(tái)計(jì)算機(jī)通過(guò)瀏覽器請(qǐng)求某個(gè)頁(yè)面時(shí),就會(huì)發(fā)送這個(gè) cookie。你可以使用 JavaScript 來(lái)創(chuàng)建和取回 cookie 的值。
添加Cookie
描述:
????新建一條Cookie,交由瀏覽器管理!
參數(shù)說(shuō)明:
- name - 鍵值對(duì)的鍵,唯一標(biāo)記一個(gè)值
- value - 鍵值對(duì)的值,cookie存儲(chǔ)的內(nèi)容
- expdays - cookie過(guò)期時(shí)間(有效時(shí)間)
function setCookie ( name, value, expdays )
{
var expdate = new Date();
//設(shè)置Cookie過(guò)期日期
expdate.setDate(expdate.getDate() + expdays) ;
//添加Cookie
document.cookie = name + "=" + escape(value) + ";expires=" + expdate.toUTCString();
}
獲取Cookie
描述:
????根據(jù)參數(shù)name,獲取cookie里面對(duì)應(yīng)的value值
function getCookie ( name )
{
//獲取name在Cookie中起止位置
var start = document.cookie.indexOf(name+"=") ;
if ( start != -1 )
{
start = start + name.length + 1 ;
//獲取value的終止位置
var end = document.cookie.indexOf(";", start) ;
if ( end == -1 )
end = document.cookie.length ;
//截獲cookie的value值,并返回
return unescape(document.cookie.substring(start,end)) ;
}
return "" ;
}
刪除Cookie
描述:
????根據(jù)name,刪除一條cookie(設(shè)置立即過(guò)期)
function delCookie ( name )
{
setCookie ( name, "", -1 ) ;
}