cookieLibrary.js 写入cookie的JavaScript代码库
js code:
1
/* Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96)
2
缔友计算机信息技术有限公司,涂聚文 geovindu@163.com 互相交流
3
Written by: Bill Dortch, hIdaho Design <geovindu@163.com>
4
The following functions are released to the public domain.
5
http://www.dusystem.com/
6
This version takes a more aggressive approach to deleting
7
cookies. Previous versions set the expiration date to one
8
millisecond prior to the current time; however, this method
9
did not work in Netscape 2.02 (though it does in earlier and
10
later versions), resulting in "zombie" cookies that would not
11
die. DeleteCookie now sets the expiration date to the earliest
12
usable date (one second into 1970), and sets the cookie's value
13
to null for good measure.
14
15
Also, this version adds optional path and domain parameters to
16
the DeleteCookie function. If you specify a path and/or domain
17
when creating (setting) a cookie**, you must specify the same
18
path/domain when deleting it, or deletion will not occur.
19
20
The FixCookieDate function must now be called explicitly to
21
correct for the 2.x Mac date bug. This function should be
22
called *once* after a Date object is created and before it
23
is passed (as an expiration date) to SetCookie. Because the
24
Mac date bug affects all dates, not just those passed to
25
SetCookie, you might want to make it a habit to call
26
FixCookieDate any time you create a new Date object:
27
28
var theDate = new Date();
29
FixCookieDate (theDate);
30
31
Calling FixCookieDate has no effect on platforms other than
32
the Mac, so there is no need to determine the user's platform
33
prior to calling it.
34
35
This version also incorporates several minor coding improvements.
36
37
**Note that it is possible to set multiple cookies with the same
38
name but different (nested) paths. For example:
39
40
SetCookie ("color","red",null,"/outer");
41
SetCookie ("color","blue",null,"/outer/inner");
42
43
However, GetCookie cannot distinguish between these and will return
44
the first cookie that matches a given name. It is therefore
45
recommended that you *not* use the same name for cookies with
46
different paths. (Bear in mind that there is *always* a path
47
associated with a cookie; if you don't explicitly specify one,
48
the path of the setting document is used.)
49
50
Revision History:
51
52
"Toss Your Cookies" Version (22-Mar-96)
53
- Added FixCookieDate() function to correct for Mac date bug
54
55
"Second Helping" Version (21-Jan-96)
56
- Added path, domain and secure parameters to SetCookie
57
- Replaced home-rolled encode/decode functions with Netscape's
58
new (then) escape and unescape functions
59
60
"Free Cookies" Version (December 95)
61
62
63
For information on the significance of cookie parameters, and
64
and on cookies in general, please refer to the official cookie
65
spec, at:
66
67
http:www.netscape.com/newsref/std/cookie_spec.html
68
69
****************************************************************** */
70
71
/* "Internal" function to return the decoded value of a cookie */
72
function getCookieVal (offset) {
73
var endstr = document.cookie.indexOf (";", offset);
74
if (endstr == -1) {
75
endstr = document.cookie.length;
76
}
77
return unescape(document.cookie.substring(offset, endstr));
78
}
79
80
/* Function to correct for 2.x Mac date bug. Call this function to
81
fix a date object prior to passing it to SetCookie.
82
IMPORTANT: This function should only be called *once* for
83
any given date object! See example at the end of this document. */
84
function FixCookieDate (date) {
85
var base = new Date(0);
86
var skew = base.getTime(); // dawn of (Unix) time - should be 0
87
if (skew > 0) { // except on the Mac - ahead of its time
88
date.setTime(date.getTime() - skew);
89
}
90
}
91
92
/* Function to return the value of the cookie specified by "name".
93
name - String object containing the cookie name.
94
returns - String object containing the cookie value, or null if
95
the cookie does not exist. */
96
function GetCookie (name) {
97
var temp = name + "=";
98
var tempLen = temp.length;
99
var cookieLen = document.cookie.length;
100
var i = 0;
101
while (i < cookieLen) {
102
var j = i + tempLen;
103
if (document.cookie.substring(i, j) == temp) {
104
return getCookieVal(j);
105
}
106
i = document.cookie.indexOf(" ", i) + 1;
107
if (i == 0) break;
108
}
109
return null;
110
}
111
112
/* Function to create or update a cookie.
113
name - String object containing the cookie name.
114
value - String object containing the cookie value. May contain
115
any valid string characters.
116
[expiresDate] - Date object containing the expiration data of the cookie. If
117
omitted or null, expires the cookie at the end of the current session.
118
[path] - String object indicating the path for which the cookie is valid.
119
If omitted or null, uses the path of the calling document.
120
[domain] - String object indicating the domain for which the cookie is
121
valid. If omitted or null, uses the domain of the calling document.
122
[secure] - Boolean (true/false) value indicating whether cookie transmission
123
requires a secure channel (HTTPS).
124
125
The first two parameters are required. The others, if supplied, must
126
be passed in the order listed above. To omit an unused optional field,
127
use null as a place holder. For example, to call SetCookie using name,
128
value and path, you would code:
129
130
SetCookie ("myCookieName", "myCookieValue", null, "/");
131
132
Note that trailing omitted parameters do not require a placeholder.
133
134
To set a secure cookie for path "/myPath", that expires after the
135
current session, you might code:
136
137
SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true); */
138
function SetCookie (name,value,expiresDate,path,domain,secure) {
139
document.cookie = name + "=" + escape (value) +
140
((expiresDate) ? "; expires=" + expiresDate.toGMTString() : "") +
141
((path) ? "; path=" + path : "") +
142
((domain) ? "; domain=" + domain : "") +
143
((secure) ? "; secure" : "");
144
}
145
146
/* Function to delete a cookie. (Sets expiration date to start of epoch)
147
name - String object containing the cookie name
148
path - String object containing the path of the cookie to delete. This MUST
149
be the same as the path used to create the cookie, or null/omitted if
150
no path was specified when creating the cookie.
151
domain - String object containing the domain of the cookie to delete. This MUST
152
be the same as the domain used to create the cookie, or null/omitted if
153
no domain was specified when creating the cookie. */
154
function DeleteCookie (name,path,domain) {
155
if (GetCookie(name)) {
156
document.cookie = name + "=" +
157
((path) ? "; path=" + path : "") +
158
((domain) ? "; domain=" + domain : "") +
159
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
160
}
161
}
162
163
164
// Calling examples:
165
// var expdate = new Date ();
166
// FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
167
// expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now
168
// SetCookie ("ccpath", "http://www.dupcit.com/articles/", expdate);
169
// SetCookie ("ccname", "WebWoman", expdate);
170
// SetCookie ("tempvar", "This is a temporary cookie.");
171
// SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
172
// SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
173
// SetCookie ("goner", "This cookie must die!");
174
// document.write (document.cookie + "<br>");
175
// DeleteCookie ("goner");
176
// document.write (document.cookie + "<br>");
177
// document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
178
// document.write ("ccname = " + GetCookie("ccname") + "<br>");
179
// document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
180
181

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Ajax&JavaScript
标签:
cookie
, JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!