html - JavaScript cookies not saving - Stack Overflow

I had a project in my web design class this semester(I'm a newbie at this) and I've been work

I had a project in my web design class this semester(I'm a newbie at this) and I've been working on a little side work in my free time. I've been trying to work with cookies and JavaScript. We didn't cover cookies very much in my class and I've been searching and searching for an answer to my current problem. Chances are, if you've ever responded to anything on this site about JavaScript and/or HTML, I've read it. As my title says, I'm having a problem where my cookie doesn't seem to save. On first load, the code works fine, then I reload the page and it doesn't recall the cookie value. This is my cookie script:

var name;  
if(document.cookie)  
{  
var q1=unescape(document.cookie);  
var q2=q1.search("name");  
if(q2!=(-1))  
    {  
        var n=str.split("name=");  
        var n1=n[1];  
        var n2=n1.split(";");  
        var q=n2[0];  
        name=q;  
    }  
    else  
    {  
        name=window.prompt("Please enter your name.");  
        document.cookie="name="+escape(name);  
    }  
 }     
else  
{  

name=window.prompt("Please enter your name.");  
    document.cookie="name="+escape(name);  
} 

This is used once the rest of the page is loaded in:

document.writeln(name+"text stuff");

In the case of an incorrect value in the cookie, I've created this function that is called on via a button in the HTML:

function wrongPerson()  
{  
    document.cookie="name=hooper; expires=Thu, 01 Jan 1970 00:00:01 GMT";  
    location.reload();  
}  

Sorry for such a long post, but I wanted to make sure the munity had as much information as possible. Also, sorry about the poor formatting. I wasn't sure how to bring over my entire long thing of code and have the system allow it.

I had a project in my web design class this semester(I'm a newbie at this) and I've been working on a little side work in my free time. I've been trying to work with cookies and JavaScript. We didn't cover cookies very much in my class and I've been searching and searching for an answer to my current problem. Chances are, if you've ever responded to anything on this site about JavaScript and/or HTML, I've read it. As my title says, I'm having a problem where my cookie doesn't seem to save. On first load, the code works fine, then I reload the page and it doesn't recall the cookie value. This is my cookie script:

var name;  
if(document.cookie)  
{  
var q1=unescape(document.cookie);  
var q2=q1.search("name");  
if(q2!=(-1))  
    {  
        var n=str.split("name=");  
        var n1=n[1];  
        var n2=n1.split(";");  
        var q=n2[0];  
        name=q;  
    }  
    else  
    {  
        name=window.prompt("Please enter your name.");  
        document.cookie="name="+escape(name);  
    }  
 }     
else  
{  

name=window.prompt("Please enter your name.");  
    document.cookie="name="+escape(name);  
} 

This is used once the rest of the page is loaded in:

document.writeln(name+"text stuff");

In the case of an incorrect value in the cookie, I've created this function that is called on via a button in the HTML:

function wrongPerson()  
{  
    document.cookie="name=hooper; expires=Thu, 01 Jan 1970 00:00:01 GMT";  
    location.reload();  
}  

Sorry for such a long post, but I wanted to make sure the munity had as much information as possible. Also, sorry about the poor formatting. I wasn't sure how to bring over my entire long thing of code and have the system allow it.

Share Improve this question edited Dec 7, 2012 at 13:18 Felipe Sabino 18.2k6 gold badges79 silver badges112 bronze badges asked Dec 7, 2012 at 13:16 user1885440user1885440 311 silver badge4 bronze badges 6
  • what browser are you testing it? – Felipe Sabino Commented Dec 7, 2012 at 13:19
  • As about formatting, you can normally just copy and paste and the click the "{}" toolbar button... – Álvaro González Commented Dec 7, 2012 at 13:20
  • In the future: no one cares about your history on this site. State the problem/errors, show some code, and show what you tried. – epascarello Commented Dec 7, 2012 at 14:01
  • You should NOT unescape the cookie before you split it or find the names. Check out setting and removing cookies quirksmode/js/cookies.html#script – epascarello Commented Dec 7, 2012 at 14:04
  • Felipe - I've been testing in Google Chrome, Firefox and Internet Explorer. All are at their newest editions. Alvaro - thanks. I'll keep that in mind for next time. epascarello - Sorry about that. I'll be sure to do so next time. Also, thanks for the link. – user1885440 Commented Dec 7, 2012 at 21:51
 |  Show 1 more ment

1 Answer 1

Reset to default 3

You need to set expire on your cookie script. If you don't put an expire date to your cookie it will recognize as a "one-time cookie". And after a reload or anything, it will disappear.

Set an expire to Wed, 01 Jan 2014 00:00:01 GMT

This is my personal opinion, but I rather use window.location.href = window.location.href; instead of location.reload();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745403854a4626222.html

相关推荐

  • html - JavaScript cookies not saving - Stack Overflow

    I had a project in my web design class this semester(I'm a newbie at this) and I've been work

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信