01
<html>
02
<head>
03
<title>原生js cookie的使用实例setCookie()、getCookie()方法
</title>
04
<script
language=
"javascript"
>
05
<!--
06
function
setCookie
(){
07
var
never
=
new
Date();
08
//设置never的时间为当前时间加上十年的毫秒值
09
never
.
setTime(
never
.
getTime()
+
10
*
365
*
24
*
60
*
60
*
1000);
10
var
expString
=
"expires="
+
never
.
toGMTString()
+
";";
11
12
document
.
cookie
=
"area="
+
escape(
"北京海淀")
+
"; "
+
expString;
13
document
.
cookie
=
"zipcode=100080;";
14
15
}
// end function
16
17
20
function
getCookie(
name
){
21
var
result
=
null;
22
//对cookie信息进行相应的处理,方便搜索
23
var
myCookie
=
""
+
document
.
cookie
+
";";
24
var
searchName
=
"+name+"
=
";
25
var startOfCookie = myCookie.indexOf(searchName);
26
var endOfCookie;
27
if(satrtOfCookie != -1){
28
startOfcookie += searchName.length;
29
endOfCookie = myCookie.indexOf(";
"
,
startOfCookie);
30
result
=
(
myCookie
.
substring(
startOfCookie
,
endOfCookie));
31
}
32
return
result;
33
}
//end function
34
//-->
35
</script>
36
</head>
37
<body>
38
<script>
39
document
.
write(
document
.
cookie
+
"<br/>");
40
document
.
write(
"area is"
+
getCookie(
"area")
+
",and zipcode is"
+
getCookie(
"zipcode"));
41
document
.
write(
"<br/>");
42
</script>
43
</body>
44
</html>
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44