Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you clear transient cookies in javascript?

Status
Not open for further replies.

USMA99

Programmer
Oct 12, 2000
85
US
JS newbie here, is there a way to clear out the contents of document.cookie. I want the transient cookies blown away as if the user closed their browser, not just resetting their values back to empty strings.

I would expect something like these to work but they dont:

document.cookie = "";
document.cookie.clear();
docuemnt.cookie.reset();
document.cookie.delete();
document.cookie.length=0;

I've looked around quite a bit and don't see any mention of being able to do it. Is this possible?

thx in advance
 
<script>
void(document.cookie)
</script>

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
daang it Im at work and not paying attention try this:


<script>
void(document.cookie="");
</script>

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
not a problem

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
er, actually that didn't work. I thought that it did on first glance, but it caused a javascript error that blew away the part of the page that prints values based on the cookie, making me think the cookie was cleared.

So, can I clear the entire cookie with one statement?
 
what do you mean by "transient cookies"?

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
a 'transient cookie' (aka session cookie) is lost when you close your browser as opposed to a persistent cookie which is a file on the hard-drive. There is no expiration on transient cookies that I can tell.

 
ok then, if it's lost when you close your browser, what's the point in trying to delete it?

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
because I have a sort of shopping cart. i loop through the items and their quantities that are in the cookie, ie:

chocolatechip=3; oatmealraisin=1; ...

to see what product and quantities the user selected. If they want to start over all of the old products are there if I can't clear the cookie. Telling them to close their browser would be silly. Looping through and setting each product to nothing is not accpetable either.
 
why is looping through and setting each to nothing unacceptable? i don't think you have a choice.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
because their cart starts off empty, but each item that they add that I cannot clear will remain there but with no value (assuming i loop through and set it to nothing). What I want to know is:

is there a way to clear out the contents of
document.cookie? I want the transient cookies blown away
as if the user closed their browser, not just resetting
their values back to empty strings.

 
you must delete each key's value, and set the cookie's "expires" property to a past date.

voila:
Code:
<html>
  <head>
    <title>test</title>

    <script type="text/javascript">
      window.onload = function() {
        if (document.cookie) alert(document.cookie);
        else {
          document.cookie = "name=foo;";
        }
      }
    </script>
  </head>

  <body>
    <form>
			<input
				type="button"
				value="delete cookie"
				onclick="document.cookie = 'name=;expires=' + new Date(1974,0,1);" />
    </form>
  </body>
</html>


upon first loading the page, nothing will happen visually. refresh, and the cookie will be alerted. click "delete cookie", then reload the page, and you're back to no cookie = no alert.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Does this work for you?

Code:
document.cookie = 'yourCookieName=\'\';' + new Date().setYear(new Date().getYear()-1).toGMTString();

Just replace "yourCookieName" with the name of your cookie.

Dan
 

Heh... So much for testing my code... For some reason, it gives me a JS error. Try this one instead:

Code:
var myDate = new Date();
myDate.setFullYear(myDate.getFullYear()-1);
document.cookie = 'yourCookieName=\'\';' + myDate.toGMTString();

Dan
 
thanks guys, i think this will get it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top