javascript学习(4)cookie的使用
具体用法:
<script type="text/javascript">
function writecookie() {
var today=new Date();
today.setDate(today.getDate() + 1);
//document.cookie = 'name=张三的歌;expires='+today.toGMTString();
document.cookie = 'username=张三的歌;expires=' + today.toGMTString();
//document.cookie = 'name=张三的歌;expires=' + today.toLocaleString();//此类型的过期时间不支持
}
function getcookie() {
//读cookie
var cookie = document.cookie;
alert(cookie);
var array = cookie.split(';');
for (var item in array) {
var array1 = array[item].split('=');
if (array1[item] == 'username') {
alert(array1[1]);
}
}
//通过截取字符串的方式来获取cookie的值
// var index = cookie.indexOf('=');
// alert(cookie.substr(index + 1));
}
</script>
<input id="Button1" type="button" value="写cookie" onclick="writecookie();" />
<br />
<input id="Button2" type="button" value="读cookie" onclick="getcookie();" />