Results 1 to 5 of 5

Thread: Cookies not being kept after close browser

  1. #1
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cookies not being kept after close browser

    Hey all!

    I am having this issue with cookies. As far as I know it is not rocket science so I don't know what can be going wrong here.

    I have tested it with Chrome and Firefox. Both browsers are showing the same behavior: the 'cookied' data remains saved as long as keep the browser opened. I can even close the tab where the cookies were saved since the application stays opened. But if I close completely the program the cookies are gone.

    I thought that it could be the expire thing and double checked it but got nothing. Here how I am saving the cookies:

    HTML Code:
    function SetCookies() {
        var d = new Date().addHours(24);
        var n = d.toUTCString();
        document.cookie = "name=" + document.getElementById('name').value;
        document.cookie = "over_18=" + (document.getElementById('yover').checked ? 'Y' : 'N');
        document.cookie = "expires=" + n;
        document.cookie = "path=/";
    }
    
    Date.prototype.addHours= function(h){
        this.setHours(this.getHours()+h);
        return this;
    }
    Do you see something wrong?

    Thanks!


  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    SetCookies function is all wrong. Well not all wrong, but mostly wrong. A single cookie needs at least name and value. If you want it to persist, it also needs an expires. If you want to control the path, it also needs a path. You are setting 4 cookies there at a time. the first two are normal expires at the end of session cookies with no path. The last two have no name or value, so are either ignored, or use expires and path respectively as names. Either way, they can't do much. To see how to set javascript cookies, you can look here:

    http://www.dynamicdrive.com/forums/e...ct-Cookie-Unit

    It's always been a bit tricky, and at that link I've trued to combine as much as possible. So, any further questions, feel free to ask.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    John, thanks a lot! I am surely going to use that your neat code!



    A further small question... I am going to store a BUNCH of data in the cookies. They are simple stuff as name, age, address, etc. Am I correct to suppose that by using obvious cookie names such as 'name' or 'age' they can be easily overwritten by another site that perhaps use the same names? I mean, my info would be more secure if instead I use something like PAMName, PAMAge, etc (PAM is the acronyms of the company I work for)?

    Also I have seen that I should keep the total size of my cookies under 4K. Is this a truth or modern browsers have expanded this limit?

    Thanks!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Never hurts to make cookie names as unique as possible. However, if you include path info (the code I offer in my blog post does so automatically), it's less important. But then - what you are trying to avoid is not being overwritten by another script or program on the same domain. If it's your domain and you know you are not setting any conflicting cookies on it, you're fine. But, unless you use no other scripts from other developers that might use those common names . . . Well as I said in the beginning, "Never hurts to make cookie names as unique as possible." For example, scripts on Dynamic Drive that set cookies usually set cookies named:

    ddscriptname

    or even:

    ddscriptnamecookiedescription

    where the green and red parts are substituted for by the actual script name and a description of what the cookie does (what kind of info it holds - name, score, etc.) respectively.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Cool, John, really awesome! Thank you very much for all your support!


Similar Threads

  1. Replies: 1
    Last Post: 02-22-2012, 05:38 PM
  2. how to close a browser window
    By slammingsammy1 in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 10-28-2010, 07:09 PM
  3. Delete cookies when closing browser
    By miradoro in forum JavaScript
    Replies: 1
    Last Post: 04-15-2009, 03:26 AM
  4. plz help using java script on IE browser close
    By raj123 in forum JavaScript
    Replies: 0
    Last Post: 06-21-2008, 06:19 AM
  5. Close Browser Script
    By k12onos in forum Dynamic Drive scripts help
    Replies: 27
    Last Post: 08-12-2006, 10:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •