现在的位置: 首页 > 综合 > 正文

javascript操作cookie范例

2018年08月20日 ⁄ 综合 ⁄ 共 1154字 ⁄ 字号 评论关闭
<!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>

抱歉!评论已关闭.