六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 102|回复: 0

Cookie读写demo

[复制链接]

升级  50%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
25
 楼主| 发表于 2013-2-7 19:24:48 | 显示全部楼层 |阅读模式
<!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=utf-8" /><title>Cookie读写demo</title><script>var cookieName = "demo";/** 创建cookie */function createCookie(){    var username = document.getElementById("username").value;       var date= new Date(); // 定义时间    var days=30; // 保存天数date.setTime(date.getTime()+(days*24*60*60*1000)); // 原有时间相加后获取有效期时间        //cookie由两部分组成name=value;有效期=值    document.cookie=cookieName+"="+username+";expires="+date.toGMTString();}/** 读取cookie */function getCookie()//取cookies函数{var arr = document.cookie.match(new RegExp("(^| )"+cookieName+"=([^;]*)(;|$)"));if(arr != null){var username = arr[2];document.getElementById("username").value = username;}}</script></head><body><input type="text" id="username" name="username"><input type="button" value="生成Cookie" ><input type="button" value="读Cookie" /></body></html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表