我有這個任務來讀取,寫入,啓用cookie,以便用戶名將被存儲在一個變量,然後寫入一個cookie。我的問題是,它看起來像代碼的最後一部分工作。但是,用戶名被假設存儲到變量中的第一部分不起作用,並且我可以看到,當我運行代碼並且前兩個警告框未顯示時。這是假設打開「你好,我是你的寵物搖滾」並且在我最後一次點擊iRock後,圖像會變成一個快樂的iRock。但它沒有發生。我猜是因爲cookie沒有被讀取。有什麼建議麼。這裏是鏈接:http://ciswebs.smc.edu/cis54/tyson_schweidel /iRockChapter3.htm如何啓用Cookie
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>iRock - The Virtual Pet Rock</title>
<link href="donut_stylesheet/donut.css" rel="stylesheet" type="text/css" />
<script type ="text/javascript" >
var userName;
function resizeRock(){
document.getElementById("rockImg").style.height = (document.body.clientHeight - 100) * 0.9;
}
function greetUser(){
userName = readCookie("rock_username");
if(userName)
alert("Hello " + userName + ", I missed you.");
else
alert("Hello, I am your pet rock");
}
function touchRock(){
if(userName)
alert("I like attention," + userName + ". Thank you.");
}
userName = prompt("What is your name?", "Enter name here.");
if(userName)
alert("It is good to meet you, " + userName + ".");
writeCookie(irock_username", userName, 1 * 365);
document.getElementById("rockImg").src = "rock_happy.png";
setTimeout("document.getElementById(('rockImg').src = 'images/rock.png';", 5 * 60 * 1000);
</script>
</head>
<body onload="resizeRock(); greetUser(); onResize="resizeRock();">
<img id="rockImg" src="images/rock.png" alt="iRock" style="cursor:pointer" onClick="touchRock();" />
</body>
</html>
+1非常好的答案是爲初學者量身定做的。 –