无法在javascript中显示保存在cookie中的值

时间:2015-01-25 14:59:10

标签: javascript cookies

我正在尝试显示一个名为欢迎用户和我输入的此用户的警报 这是我的代码



<html>
    <head>
        <title>First Page A1 </title>
        <link type="text/css" rel="stylesheet" href="text.css">
		<script type="text/javascript" src = "game.js"></script>   
    </head>
    <body >
        
        <div  id="container">
			<h1>
				  Khokha Game
				</h1>
		    <div id="formContent">
				<form id="myform" action="game.html"  method="get" >
					<table>
						<tr>
                               <td>Your Name</td>
								<td><input type="text" id="username" name="username"></td>
						</tr>
      
						<tr>
                                <td>Difficulty</td>
								<td>
								<select id="difficulty" name="difficylty">
									<option value="1">Easy</option>
                                   <option value="2">Medium</option>
                                   <option value="3">Difficult</option>
                           
								</select></td>
						</tr>
						
						<tr>
								<td><input type="submit" id="submit" value="Start" onclick="checkCookie()"></td>
						</tr>
            
					</table>
				</form>
			</div>
		</div>
    </body>
	
<script>


function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return " ";
}

function checkCookie() {
    var user=getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
       user = document.getElementById("username");
	   //alert(user);
       if (user != "" && user != null) {
           setCookie("username", user, 1);
       }
    }
}

</script>		
</html>
&#13;
&#13;
&#13;

但是会发生什么是cookie没有存储用户的价值而且我不知道为什么..任何帮助? 我不能很好地

0 个答案:

没有答案