I'm studying Javascript cookie. Basically what I want is
when the first time user input their number it will save the cookie name, value and expiration date to 365.
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.