Tarek Sawah
Tarek Sawah

Reputation: 305

using document.cookie to delete cookies not working on firefox

below is a simple logout script which is triggered by clicking the "LOGOUT" anchor in the navigation bar in my website. the code works fine in chrome but doesn't do a thing in firefox.

            $('a#UserLogout').on('click', function(e){
                e.preventDefault;
                document.cookie = '_session_login=""; expires="Thu, 01-Jan-70 00:00:01 GMT";';
                location.reload();
            });

PS. I have another cookie for language preference and it is working code below:

function setCookie(cname,cvalue,exdays)
{
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
    location.reload();
}

and i tried using -365 in the exdays but didn't work as well

Upvotes: 1

Views: 4829

Answers (3)

yohny
yohny

Reputation: 172

I encountered this issue (FF 29) and solution was to also set path when setting up the cookie. It should not be needed because by specification (and also according to MDN) when path is omitted it should default to current path, however it is probably defaulting to / (root) or something else.
Anyway in my case I was deleting the cookie in domain subfolder (for example www.example.com/SubFolder/) and specifying matching path (eg. SubFolder) allowed me to delete the cookie.
Generally setting the cookie path to location.pathname should do the trick.

Upvotes: 2

epascarello
epascarello

Reputation: 207557

Get rid of the extra quotes and change it to this

document.cookie = '_session_login=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Also you should tell the browser to force the page to load from the server, not the cache

window.location.reload(true);

Upvotes: 1

Wundwin Born
Wundwin Born

Reputation: 3475

Try with 1970

document.cookie = '_session_login=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Upvotes: 1

Related Questions