个人小站

[Javascript]Cookie操作测试

字数统计: 296阅读时长: 1 min
2021/08/16

测试页面
尝试设置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>

原文作者:ted423

原文链接:http://ted423.github.io/Code/WWW/cookie/

发表日期:August 16th 2021, 7:33:38 pm

更新日期:February 19th 2016, 3:59:00 pm

版权声明:本站原创内容(一般是语句不通顺的那种)采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,转载内容以及不带个人观点的分享不在此例,摘抄有Wiki的内容的文章统一根据Wiki采用 CC BY-SA 3.0

CATALOG