功能:设置或查询与当前文档相关的所有cookie。
语法:document.cookie
该属性是一个可读可写的字符串,可使用该属性对当前文档的cookie进行读取、创建、修改和删除操作
创建Cookie。
1
<
script type
=
"
text/javascript
"
>
2 function setCookie()
3 {
4 var d = new Date();
5 document.cookie = " time= " + d.toLocaleString();
6 }
7 < / script>
8
9 < input type = " button " value = " 创建Cookie " onclick = " setCookie() " / >
2 function setCookie()
3 {
4 var d = new Date();
5 document.cookie = " time= " + d.toLocaleString();
6 }
7 < / script>
8
9 < input type = " button " value = " 创建Cookie " onclick = " setCookie() " / >
读取Cookie。
1
<
script type
=
"
text/javascript
"
>
2 function getCookie()
3 {
4 if ( document.cookie.length > 0 )
5 {
6 tstart = document.cookie.indexOf( " time= " );
7 if ( tstart !=- 1 )
8 {
9 tstart += 5 ;
10 tend = document.cookie.indexOf( " ; " ,tstart);
11 if ( tend ==- 1 ) tend = document.cookie.length;
12 alert( ' 欢迎回来!\n您上次的访问时间是: ' + document.cookie.substring(tstart,tend) );
13 }
14 }
15 }
16 < / script>
17
18 < input type = " button " value = " 欢迎 " onclick = " getCookie() " / >
2 function getCookie()
3 {
4 if ( document.cookie.length > 0 )
5 {
6 tstart = document.cookie.indexOf( " time= " );
7 if ( tstart !=- 1 )
8 {
9 tstart += 5 ;
10 tend = document.cookie.indexOf( " ; " ,tstart);
11 if ( tend ==- 1 ) tend = document.cookie.length;
12 alert( ' 欢迎回来!\n您上次的访问时间是: ' + document.cookie.substring(tstart,tend) );
13 }
14 }
15 }
16 < / script>
17
18 < input type = " button " value = " 欢迎 " onclick = " getCookie() " / >
查看Cookie。
1
<
script type
=
"
text/javascript
"
>
2 document.write( document.cookie );
3 < / script>
4
2 document.write( document.cookie );
3 < / script>
4