Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

$.cookie('name', null) not deleting cookie one place, is another... ? #45

Closed
thewatts opened this issue Jan 20, 2012 · 3 comments
Closed

Comments

@thewatts
Copy link

So, I have duplicate code here.... just attached to two different selectors (based on where you are located on site).

This code will not work for the $.cookie('name', null) to delete cookie.

$('a.campus-select').click(function(){
    var $campus = $(this).attr('rel');

    // set or delete cookie
    if ($campus == "all") {
        $.cookie('Campus', null); // TRYING TO DELETE COOKIE - BUT WONT HERE. WILL BELOW?
        console.log("current campus is " + $campus);
        console.log($.cookie('Campus'));
    } else { 
        $.cookie('Campus', null);
        // setCookie('Campus',$campus,365); 
        $.cookie('Campus', $campus, { expires: 365, path: '/' });
        console.log("just set the cookie for /");
    }
});

This code will work work for the $.cookie('name', null) to delete cookie.

$('a.home-campus-select').click(function(){
    var $campus = $(this).attr('rel');
    var $update_links = $('ul.filter li a');

    // set or delete cookie
    if ($campus == "all") {
        $.cookie('Campus', null); // BUT DELETING A COOKIE HERE WORKS... ?
        console.log("current campus is " + $campus);
    console.log($.cookie('Campus'));
    } else { 
        $.cookie('Campus', null);
    // setCookie('Campus',$campus,365); 
    $.cookie('Campus', $campus, { expires: 365, path: '/' });
    console.log("just set the cookie for /");
    }
});

I have literally copied and pasted the code into each other - they are nearly identical - yet - the top will not delete the cookie with $.cookie('Campus', null);

I may be completely missing this - and if so, that is fine :) I just want to know what I'm doing wrong - or if there is a bug!

If you want to see it in action: 2011.newlifechurch.tv

Working one is on the ** home page ** - bottom updates, you can see in the console. When clicking 'All' in the filter - you will see null show up (make sure you select a campus first!)

Then on the updates page (2011.newlifechurch.tv/updates) - if you do the same with the filters, you will see that it won't say 'null', instead saying a campus name.

@carhartl
Copy link
Owner

When deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie.

@carhartl
Copy link
Owner

e.g. if you're not on the root level of your page, to delete the cookie (being allowed to delete that is from a browser perspective), you need to include the path when deleting.

$.cookie('Campus', null, { path: '/' });

@thewatts
Copy link
Author

Great!

Thanks so much for the help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants