回答編集履歴
3
yori
test
CHANGED
@@ -89,3 +89,83 @@
|
|
89
89
|
};
|
90
90
|
|
91
91
|
````
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
# より具体的に
|
96
|
+
|
97
|
+
```javascript
|
98
|
+
|
99
|
+
<script>
|
100
|
+
|
101
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
102
|
+
|
103
|
+
console.log(document.cookie);
|
104
|
+
|
105
|
+
document.querySelector('#hoge').value=getCookie("hoge");
|
106
|
+
|
107
|
+
document.querySelector('#set').addEventListener('click',function(e){
|
108
|
+
|
109
|
+
var d=new Date();
|
110
|
+
|
111
|
+
d.setSeconds(d.getSeconds()+5);
|
112
|
+
|
113
|
+
setCookie("hoge",document.querySelector('#hoge').value,d);
|
114
|
+
|
115
|
+
});
|
116
|
+
|
117
|
+
});
|
118
|
+
|
119
|
+
function setCookie(key,val,expiredate){
|
120
|
+
|
121
|
+
var tmp = key+"="+escape(val)+";";
|
122
|
+
|
123
|
+
var expires="";
|
124
|
+
|
125
|
+
if(expiredate){
|
126
|
+
|
127
|
+
expires=" expires="+expiredate.toGMTString();
|
128
|
+
|
129
|
+
}else{
|
130
|
+
|
131
|
+
expires= " expires=Fri, 31 Dec 2030 23:59:59 GMT;";
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
console.log(expires);
|
136
|
+
|
137
|
+
tmp += expires;
|
138
|
+
|
139
|
+
document.cookie = tmp;
|
140
|
+
|
141
|
+
};
|
142
|
+
|
143
|
+
function getCookie(key){
|
144
|
+
|
145
|
+
var params = document.cookie.split(/; */);
|
146
|
+
|
147
|
+
var ret="";
|
148
|
+
|
149
|
+
for(var i=0;i<params.length;i++){
|
150
|
+
|
151
|
+
if(r=params[i].match(new RegExp("^"+key+"="+"(.*)"))){
|
152
|
+
|
153
|
+
ret=r[1];
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
return ret;
|
160
|
+
|
161
|
+
};
|
162
|
+
|
163
|
+
</script>
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
<input type="text" name="hoge" value="" id="hoge">
|
168
|
+
|
169
|
+
<input type="button" value="set" id="set">
|
170
|
+
|
171
|
+
```
|
2
chousei
test
CHANGED
@@ -62,8 +62,6 @@
|
|
62
62
|
|
63
63
|
}
|
64
64
|
|
65
|
-
console.log(expires);
|
66
|
-
|
67
65
|
tmp += expires;
|
68
66
|
|
69
67
|
document.cookie = tmp;
|
1
sample
test
CHANGED
@@ -17,3 +17,77 @@
|
|
17
17
|
```
|
18
18
|
|
19
19
|
になっています。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
# sample
|
24
|
+
|
25
|
+
このサンプルで3秒間だけhogeの値に123が表示されますか?
|
26
|
+
|
27
|
+
私の環境だとIE5エミュレータでも動きますけど
|
28
|
+
|
29
|
+
```javascript
|
30
|
+
|
31
|
+
var d=new Date();
|
32
|
+
|
33
|
+
d.setSeconds(d.getSeconds()+4);
|
34
|
+
|
35
|
+
setCookie("hoge",123,d);
|
36
|
+
|
37
|
+
var count=0;
|
38
|
+
|
39
|
+
var timerId=setInterval(function(){
|
40
|
+
|
41
|
+
console.log(count+":hoge="+getCookie("hoge"));
|
42
|
+
|
43
|
+
if(++count>5) clearInterval(timerId);
|
44
|
+
|
45
|
+
},1000);
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
function setCookie(key,val,expiredate){
|
50
|
+
|
51
|
+
var tmp = key+"="+escape(val)+";";
|
52
|
+
|
53
|
+
var expires="";
|
54
|
+
|
55
|
+
if(expiredate){
|
56
|
+
|
57
|
+
expires=" expires="+expiredate.toGMTString();
|
58
|
+
|
59
|
+
}else{
|
60
|
+
|
61
|
+
expires= " expires=Fri, 31 Dec 2030 23:59:59 GMT;";
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
console.log(expires);
|
66
|
+
|
67
|
+
tmp += expires;
|
68
|
+
|
69
|
+
document.cookie = tmp;
|
70
|
+
|
71
|
+
};
|
72
|
+
|
73
|
+
function getCookie(key){
|
74
|
+
|
75
|
+
var params = document.cookie.split(/; */);
|
76
|
+
|
77
|
+
var ret=null;
|
78
|
+
|
79
|
+
for(var i=0;i<params.length;i++){
|
80
|
+
|
81
|
+
if(r=params[i].match(new RegExp("^"+key+"="+"(.*)"))){
|
82
|
+
|
83
|
+
ret=r[1];
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
return ret;
|
90
|
+
|
91
|
+
};
|
92
|
+
|
93
|
+
````
|