我正在学习Javascript cookie。基本上我想要的是处理Javascript Cookie
当第一次用户输入他们的人数将保存cookie名称,值和到期日期365
当用户再次访问我的网站,他/她并不只要cookie仍然存在或尚未在浏览器中删除,就不必再输入他的号码,他/她将被重定向到我的主页。
这里是我的JavaScript代码至今:
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.");
}
我的html身体
<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>
它所有的作品,但在我的萤火虫:
Uncaught TypeError: Cannot read property 'value' of undefined
set_name
checkCookie
(anonymous function)
onload
也许我的编码风格是错误的。我打开重写我的代码。我还是新来的JavaScript。
如何说firebug错误:Uncaught TypeError:无法读取未定义的属性'值'。这只有在用户仍然没有输入他的号码并将cookie保存在浏览器中时才会发生。 – 2012-02-01 05:03:07