function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chris_parxchris_parx 

Cookie deletion

Hello!

 

I'm triing to delete a cookie after a user log out but well, I don't find how to do that. Of course, the cookie is deleted when the browser is closed but if the user log again (and do not close the browser), the cookie is kept, which is not wished.

An alternative would be to store the session Id in the cookie but it appears that the session Id is different  for each page meaning it's not reliable (I thought that the sessionId is the same for the whole "Session" but well, it is not).

 

Finally, what I'm triing to do: a teaser should be showed on the first page when the user log in. If the user clicks on a button, this teaser is hidden for the whole session. But if the user log out and log in again, this teaser should be showed again! It works as intended if the user closes its browser... but only on this condition unfortunately :(

 

any idea how to solve that ?

 

Thank you!!

Ronen.ax767Ronen.ax767

Hi,

 

Just make new cookie with the same name , it's should clear your cookie .

 

here is some code that will make your job when user is logged out

 

If you handle cookies on SF:


           // create a new cookie with name 'MYCookie', an initial value of Name at the page,   
           // path 'null', maxAge '-1', and isSecure 'false'. 
           Cookie myCookies=new Cookie('MYCookie','',null,-1,true);

 

If you handle cookies on JS:


    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/ ";
    }
 
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }

 

hope this helps.

chris_parx2chris_parx2

 


Ronen wrote:

Hi,

 

Just make new cookie with the same name , it's should clear your cookie .

 

here is some code that will make your job when user is logged out

 

If you handle cookies on SF:


           // create a new cookie with name 'MYCookie', an initial value of Name at the page,   
           // path 'null', maxAge '-1', and isSecure 'false'. 
           Cookie myCookies=new Cookie('MYCookie','',null,-1,true);

 

If you handle cookies on JS:


    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/ ";
    }
 
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }

 

hope this helps.


Thank you for your answer but well, how do you know when the user log out ? It's indeed rather my problem. Because this "log out" is done automatically by Salesforce and I don't do anything.

 

Ronen.ax767Ronen.ax767

You can't take over the "log out" of SF but you can attach a session ID for each cookie, that will helps you to handle session cookie's

 

On the other hand you can handle this JS

you can delete the cookies when the page is redirect

 

Add in body Tag:

 

<body onunload="removeCookies()">

and this js function:

 

<script type="text/JavaScript">             
function removeCookies() {
eraseCookie("MySFCookie");

} </script>
chris_parx2chris_parx2

 

it's also my problem I wrote in my first post: The session Id is different for each page, not for each Session, meaning I cannot recognize I'm still in the same "session". It's a really strange behaviour. it would be else a really good solution, however it does not work.
On another hand, I cannot use this "removeCookie()" because I don't know the place where the user will log out (on which page?) - and the cookie should be kept alive as long as the user is not logged out.

Ronen wrote:

You can't take over the "log out" of SF but you can attach a session ID for each cookie, that will helps you to handle session cookie's

 

 

Ronen.ax767Ronen.ax767

It's no matter where the user will logged out the function will run.

and about the session there is a session for the logging

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_login_loginresult.htm#topic-title

see under sessionId  function