JavaScript學(xué)習(xí)筆記之Cookie對象
JavaScript Cookie
Cookie對象:
Cookie是一種以文件的形式保存在客戶端硬盤的Cookies文件夾中的用戶數(shù)據(jù)信息(Cookie數(shù)據(jù))。
Cookie文件由所訪問的Web站點建立,以長久的保存客戶端與Web站點間的會話數(shù)據(jù),并且該Cookie數(shù)據(jù)只允許被所訪問的Web站點進(jìn)行讀取。
Cookie文件的格式:
NS:Cookie.txt
IE:用戶名@域名.txt
有兩種類型的cookie:
(1)持久性cookie,會被存儲到客戶端的硬盤上。
(2)會話Cookie:不會存儲到客戶端的硬盤上,而是放在瀏覽器進(jìn)程所處的內(nèi)存當(dāng)中,當(dāng)瀏覽器關(guān)閉則該會話cookie就銷毀了。
用JS實現(xiàn)Cookie操作
寫入Cookie:
document.cookie = " 關(guān)鍵字 = 值 [ ; expires = 有效日期 ] [;...]"
讀取Cookie:
document.cookie
刪除Cookie:
document.cookie = " 關(guān)鍵字 = ; expires = 當(dāng)前日期"
備注:
1.有效日期格式:Wdy,DD-Mon-YY HH:MM:SS GMT
2.Wdy / Mon:英文星期 / 月份;
3.還包含path、domain、secure屬性;
4.每個Web站點(domain)可建立20個Cookie數(shù)據(jù);
5.每個瀏覽器可存儲300個Cookie數(shù)據(jù),4k字節(jié);
6.客戶有權(quán)禁止Cookie數(shù)據(jù)的寫入。
實例
<!DOCTYPE html>
<html>
<head>
<title>cookieTest.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
var today = new Date();
var expiredDay = new Date();
var msPerMonth = 1000 * 60 * 60 * 24 * 30;
expiredDay.setTime(today.getTime() + msPerMonth); //一個月之后過期
//寫入cookie
document.cookie = "name=mengdd;expires="+expiredDay.toGMTString();
document.writeln("cookie已經(jīng)寫到硬盤上了");
//讀取cookie
document.writeln("內(nèi)容是:" + document.cookie);
document.writeln("expire day: " + expiredDay.toGMTString());
</script>
</body>
</html>
以上就是javascript中cookie對象的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
第一個JavaScript入門基礎(chǔ) document.write輸出
關(guān)于JavaScript,他是一個OOSP(面向?qū)ο竽_本語言)他是用來創(chuàng)建動態(tài)網(wǎng)站,增強用戶界面的一門技術(shù)。如果你想了解更多關(guān)于JavaScript的信息,請去維基百科查詢。2010-02-02
JavaScript for循環(huán) if判斷語句(學(xué)習(xí)筆記)
下面小編就為大家?guī)硪黄狫avaScript for循環(huán) if判斷語句(學(xué)習(xí)筆記)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
JavaScript初學(xué)者應(yīng)注意的七個細(xì)節(jié)詳細(xì)介紹
種種語言都有它特別的地方,對于JavaScript來說,使用var就可以聲明任意類型的變量,這門腳本語言看起來很簡單,然而想要寫出優(yōu)雅的代碼卻是需要不斷積累經(jīng)驗的,接下來介紹初學(xué)者應(yīng)注意2012-12-12

