Manejo de cookies de JavaScript

I'm studying Javascript cookie. Basically what I want is

  1. when the first time user input their number it will save the cookie name, value and expiration date to 365.

  2. When the user visit again my site he/she don't have to enter his number anymore as long as the cookie is still alive or haven't deleted yet in the browser, he/she will be redirected to my homepage.

Here's my code in javascript so far:

function checkCookie() {
    var mob_tel=getCookie("mob_tel");
    if (mob_tel!=null && mob_tel!="") {
        alert("Welcome again " + mob_tel);
    } else {
        set_name("");
    }
}

function set_name(form) {
    var mob_tel = form.mobtel.value
    if (mob_tel != "") {
        if (confirm("Are you sure you want this saved as your number?")) {
            setCookie ("mob_tel", mob_tel, 365);
            //window.history.go(0);
        }
    } else alert("Geez, at least enter something, entering nothing will cause an error.");
}

my html body

<body onload="checkCookie()">
    <form>
            Enter Mobile Number: <input type="text" name="mobtel"><br>
            <input type="submit" value="Save Cookie" onclick="set_name(this.form)">
    </form>
</body>

It all works but in my firebug:

Uncaught TypeError: Cannot read property 'value' of undefined
set_name
checkCookie
(anonymous function)
onload

Maybe my coding style is wrong. I'm open to rewrite my code. I'm still new to javascript.

preguntado el 01 de febrero de 12 a las 04:02

2 Respuestas

Add name to you form:


<form name="your_form_name_here">
    Enter Mobile Number: <input type="text" name="mobtel" value=""><br>
    <input type="submit" value="Save Cookie" onclick="set_name(this.form)">
</form>

Respondido 01 Feb 12, 08:02

How about the firebug error which says: Uncaught TypeError: Cannot read property 'value' of undefined. This only occur if a user still haven't input his number and saved the cookies in the browser. - Codificador preguntándose

Here's a working version.

Esto es lo que hice:
1 - getCookie() y la setCookie() weren't defined. I used the W3Schools functions to supplement them.
2 - There were some scripting errors (missing semi-colons,etc) I fixed those.
3 - for the set_name() function, I changed it to a DOM query to access the input which held the telephone number.

<script>
function checkCookie() {
    var mob_tel=getCookie("mob_tel");
    if (mob_tel!=null && mob_tel!="") {
        alert("Welcome again " + mob_tel);
    } else {
        set_name("");
    }
}

function getCookie(c_name) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) {
            return unescape(y);
        }
    }
}

function setCookie(c_name,value,exdays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function set_name(form) {
    var mob_tel = document.getElementById('mobtel').value;
    if (mob_tel != "") {
        if (confirm("Are you sure you want this saved as your number?")) {
            setCookie ("mob_tel", mob_tel, 365);
            //window.history.go(0);
        }
    } else alert("Geez, at least enter something, entering nothing will cause an error.");
}
</script>
<body onload="checkCookie()">
    <form>
            Enter Mobile Number: <input type="text" name="mobtel" id="mobtel"><br>
            <input type="submit" value="Save Cookie" onclick="set_name(this.form)">
    </form>
</body>

Respondido 01 Feb 12, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.