Javascript 从文本框创建cookie

Javascript 从文本框创建cookie,javascript,html,cookies,Javascript,Html,Cookies,我想练习创建cookies,我在互联网上找到了很多示例。我试着自己实现代码,但当我测试它时,什么也没发生。代码如下: <!DOCTYPE html> <html> <head> <script> function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime() + (exday

我想练习创建cookies,我在互联网上找到了很多示例。我试着自己实现代码,但当我测试它时,什么也没发生。代码如下:

<!DOCTYPE html>
<html>
<head>
    <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 cookieText() {
                var username = document.getElementByID("sometext").value;
                    setCookie("username", user, 30);
            }

            function showCookie() {
                var user=getCookie("username");
                    alert("Hello " + user);
            }

    </script>
</head>

<body>
    <form>
        <input type="text" id="someText"></input>
        <input type="button" value="create cookie!" onClick="cookieText();"></input>
        <input type="button" value="show the cookie!" onClick = "showCookie();"></input>
    </form>
</body>
</html>

函数setCookie(cname、cvalue、exdays){
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.togmString();
document.cookie=cname+“=”+cvalue+”;“+expires;
}
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);

对于(var i=0;ILOT of issues-它是getElementById而不是ByID,您没有在任何地方定义“user”,sometext应该是“sometext”等。请在需要时检查控制台工具debugging@daniel谢谢你指出这些。我会重做整件事。不用担心,也许是一个有用的链接(了解它们会节省你很多时间):@daniel非常感谢你的提示:)祝你今天愉快