19

Possible Duplicate:
Clear cookies on browser close

When I close the browser, I want to remove cookies. How can I do this?

Community
  • 1
  • 1
Osama khodroj
  • 1,230
  • 2
  • 23
  • 29
  • 2
    I think if you set the expiration date to one in the past, the cookies will be deleted once you close the browser. – JCOC611 Feb 13 '11 at 17:33
  • 2
    Have you thought of using a session cookie instead that automatically expires when the browser is closed? – Gumbo Feb 13 '11 at 17:36
  • 2
    Please clarify your question. By "I" do you mean the user or the programmer? Either one can remove cookies when the browser closes. – John Pick Feb 13 '11 at 18:35

3 Answers3

48

If you don't set an expiry date it defaults to expire at the end of the session . Refer this links -

jscookies

Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75
Nick
  • 3,096
  • 3
  • 20
  • 25
7

taken from here

function del_cookie(name) {
    document.cookie = name +
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }

Now all you need to do is to call this del_cookie() function passing it the name of whatever cookie it is that we wish to delete. The function will update the expiry date on the cookie to one long in the past so that the cookie will be considered to be expired and will be ignored by the browser exactly the same as if it didn't exist

to be used something like

<body onload="SetCookie()" onunload="del_cookie()">
ayush
  • 14,350
  • 11
  • 53
  • 100
2

You are looking for session cookies.

A tutorial on hwo to create them in JavaScript is available here.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121