<!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" οnclick="setCookie()" />
保存Cookie
<label>
<input type="button" name="Submit" value="显示" οnclick="showValue()" />
</label>
<label>
<input type="button" name="Submit2" value="清除"
οnclick="clearValue()" />
</label>
</form>
</body>
</html>
javascript操作cookie范例
最新推荐文章于 2024-04-30 16:57:21 发布