您的位置:首页 > Web前端 > JavaScript

javascript操作cookie范例

2011-12-06 10:13 381 查看
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
//直接复制拷贝 运行可以看到效果
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Cookie的使用方法</title>
<script language="javascript" type="text/javascript">
//检查Cookie是否有效
function setCookie() {

if (!navigator.cookieEnabled) {

alert('不Cookie项!');

} else {

var date = new Date();

date.setTime(date.getTime() + 60000 * 10);

document.cookie = 'userName=' + escape(document.form1.userName.value)
+ ';expires=' + date.toGMTString() + ';path=/'
+ ';domaim=zgdsc.cn' + ':secure';

}

}

function showValue() {

alert(unescape(document.cookie));

}

function clearValue() {

var date = new Date();

date.setTime(date.getTime() - 1);

document.cookie = 'userName=' + escape(document.form1.userName.value)
+ ';expires=' + date.toGMTString() + ';path=/' + ';domain=zgdsc.cn'
+ ';secure';

}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="#">
输入用户名:
<input type="text" name="userName" />
<input type="radio" name="cookieEnabled" value="radiobutton" onclick="setCookie()" />
保存Cookie
<label>
<input type="button" name="Submit" value="显示" onclick="showValue()" />
</label>
<label>
<input type="button" name="Submit2" value="清除"
onclick="clearValue()" />
</label>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: