- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- <script type="text/javascript">
- function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
- {
- var Days = 1; //此 cookie 将被保存 1 天
- var exp = new Date(); //new Date("December 31, 9998");
- exp.setTime(exp.getTime() + Days*24*60*60*1000);
- document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
- }
- function readCookie(name)
- {
- var cookieValue = "";
- var search = name + "=";
- if(document.cookie.length > 0)
- {
- offset = document.cookie.indexOf(search);
- if (offset != -1)
- {
- offset += search.length;
- end = document.cookie.indexOf(";", offset);
- if (end == -1) end = document.cookie.length;
- cookieValue = unescape(document.cookie.substring(offset, end))
- }
- }
- alert(cookieValue);
- return cookieValue;
- }
- function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
- var date = new Date();
- date.setTime(date.getTime() - 10000);
- document.cookie = name + "=a; expires=" + date.toGMTString();
- }
- </script>
- </head>
- <body>
- <input type="submit" value="写入Cookies" onclick="SetCookie('users','110')" />
- <input type="submit" value="读取Cookies" onclick="readCookie('users')" />
- <input type="submit" value="删除Cookies" onclick="delCookie('users')" />
- </body>
- </html>
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/925216,如需转载请自行联系原作者