[Javascript]Cookie操作测试

  • ~3.06K 字
  • 次阅读

测试页面
尝试设置cookies里加入test cookie

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
<script>
document.writeln("<br>")
function setCookie (cookieName, cookieValue, cookiePath, cookieExpires)
{
cookieValue = escape(cookieValue);
if (cookieExpires == "")
{
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 6);
cookieExpires = nowDate.toGMTString();
}
if (cookiePath != "")
{
cookiePath = ";Path=" + cookiePath;
}
document.cookie = cookieName + "=" + cookieValue +
";expires=" + cookieExpires + cookiePath;
}
function getCookieValue(cookieName)
{
var cookieValue = document.cookie;
var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

if (cookieStartsAt == -1)
{
cookieStartsAt = cookieValue.indexOf(cookieName + "=");
}

if (cookieStartsAt == -1)
{
cookieValue = null;
}
else
{

cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
if (cookieEndsAt == -1)
{
cookieEndsAt = cookieValue.length;
}
cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));

}

return cookieValue;
}
setCookie("TestCookie","Yes","","");
if(getCookieValue("TestCookie")==null)
document.writeln("SetCookie wrong<br>");
else document.writeln("SetCookie sucess :<br>"+document.cookie+"<br>");
</script>
打赏
打赏提示信息
分享
分享提示信息