处理Javascript Cookie [英] Handling Javascript Cookies

查看:39
本文介绍了处理Javascript Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Javascript Cookie.基本上我想要的是

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

  1. 当用户第一次输入他们的号码时,它将cookie名称,值和有效期保存到365.

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

当用户再次访问我的网站时,只要Cookie仍然有效或尚未在浏览器中删除,他/她就不必再输入其号码,他/她将被重定向到我的主页.

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.

到目前为止,这是我用javascript编写的代码:

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.");
}

我的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>

除了我的萤火虫外,其他所有方法都有效:

It all works but in my firebug:

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

也许我的编码风格是错误的.我愿意重写代码.我对javascript还是很陌生.

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

推荐答案

这是一个有效的版本.

这就是我所做的:
1-未定义 getCookie() setCookie().我使用W3Schools函数对其进行了补充.
2-我修复了一些脚本错误(缺少分号等).
3-对于 set_name()函数,我将其更改为DOM查询以访问保存电话号码的输入.

This is what I did:
1 - getCookie() and 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>

这篇关于处理Javascript Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆