Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2009, 05:16 PM   PM User | #1
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
How to make jQuery's cookies live after browser closes

http://plugins.jquery.com/files/jquery.cookie.js.txt

The cookie always deletes after browser closes. How do I fix it?
Apothem is offline   Reply With Quote
Old 12-19-2009, 10:26 PM   PM User | #2
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
As long as you've set the cookie expiry to a future date and the user does not have their browser clear all cookies on close then by all means it should be persistant.

To inspect the expiry I tend to use Firecookie in conjunction with Firebug and you can see then if the expiry is either the session or a future date.
Iszak is offline   Reply With Quote
Old 12-20-2009, 04:45 PM   PM User | #3
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
The cookie is still cleared.
Code:
jQuery.cookie = function(name, value) {
    if (typeof value != 'undefined') {
		document.cookie = name+'='+encodeURIComponent(value);
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
Did I miss something?
Apothem is offline   Reply With Quote
Old 12-21-2009, 10:07 AM   PM User | #4
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
I don't know where you got the code above from, but from the documentation in the link you initially provided:
Quote:
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. If a negative value is specified (e.g. a date in the past), the cookie will be deleted. If set to null or omitted, the cookie will be a session cookie and will not be retained when the the browser exits.
Spudhead is offline   Reply With Quote
Old 12-21-2009, 05:03 PM   PM User | #5
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
I just modified their code. I now want to know how to make it survive even after browser closes.
Apothem is offline   Reply With Quote
Old 12-22-2009, 08:21 AM   PM User | #6
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
As far as I know there is no way to guarantee that the cookie won't be cleared when the browser is closed, it's all based on the browser's preference. As long as the expiry isn't set to the session then it should be retained unless otherwise setup as previously stated.
Iszak is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 PM.


Advertisement
Log in to turn off these ads.