87,989
社区成员
发帖
与我相关
我的任务
分享<html>
<head>
<title>
cookie验证程序
</title>
<script>
function makecookie(form) {
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
when.setFullYear(when.getFullYear() + 1);
yname = form.yourname.value;
ypassword = form.yourpassword.value;
document.cookie = encodeURI("name") + "=" + encodeURI(yname) + ";expires=" + when.toGMTString();
document.cookie = encodeURI("password") + "=" + encodeURI(ypassword) + ";expires=" + when.toGMTString();
alert(document.cookie);
}
function welcome(myform) {
you = myform.yourname.value;
var position = document.cookie.indexOf("name=");
if (position != -1) {
var begin = position + 5;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = document.cookie.length;
}
you = decodeURI(document.cookie.substring(begin, end));
str = you;
alert("欢迎" + you)
} else {
alert("你没有cookie");
}
}
</script>
</head>
<body bgcoloR="green" onLoad="document.form1.reset()">
<form name="form1">
用户名:
<input type="text" name="yourname">
<p>
密码:
<input type="text" name="yourpassword">
<p>
<input type="button" value="创建cookie" onclick="makecookie(this.form);">
<p>
<input type="button" value="查看cookie" onclick="welcome(this.form);">
<p>
</form>
</body>
</html>