我正在编写基于教程的代码,并且在教程上代码有效,但是当我尝试自己做时,它没有。你能帮助我吗 ?
代码:(教程链接)。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var user_name;
var expires = new Date();
if ((document.cookie == "") == false)
{
var length = document.cookie.length - 1;
var message = document.cookie.substr(5, length);
document.write("<h2><center>Welcome back, " + message + "</center></h2>");
}
function check()
{
user_name = document.getElementById("name").value;
expires.setFullYear(expires.getFullYear() + 1);
document.cookie = escape("name") + escape(user_name) + "; expires = " + expires.toGMTString();
alert(document.cookie);
}
</script>
<meta charset="utf-8">
<title></title>
</head>
<body>
Name: <input type="text" id="name" />
<input type="button" value="Enter" onClick="check()" />
</body>
</html>