Home Page
  • December 10, 2024, 08:00:53 pm *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: Erasing Website Cookies  (Read 12343 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 551
    • View Profile
    • Dakusan's Domain
Erasing Website Cookies
« on: September 28, 2009, 05:32:06 am »

Original post for Erasing Website Cookies can be found at https://www.castledragmire.com/Posts/Erasing_Website_Cookies.
Originally posted on: 11/12/08

This erases all cookies on the current domain (in the “ / ” path)

JavaScript:

function ClearCookies() //Clear all the cookies on the current website
{
   var MyCookies=document.cookie; //Remember the original cookie string since it will be changing soon
   var StartAt=0; //The current string pointer in MyCookies
   do //Loop through all cookies
   {
      var CookieName=MyCookies.substring(StartAt, MyCookies.indexOf('=', StartAt)).replace(/^ /,''); //Get the next cookie name in the list, and strip off leading white space
      document.cookie=CookieName+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"; //Erase the cookie
      StartAt=MyCookies.indexOf(';', StartAt)+1; //Move the string pointer to the end of the current cookie
   } while(StartAt!=0)
}

I went a little further with the script after finishing this to add a bit of a visual aspect.
The following adds a textarea box which displays the current cookies for the site, and also displays the cookie names when they are erased.

<input type=button value="Clear Cookies" onclick="ClearCookies()">
<input type=button value="View Cookies" onclick="ViewCookies()">
<textarea id=CookieBox style="width:100%;height:100%"></textarea>
<script type="text/javascript">
function ViewCookies() //Output the current cookies in the textbox
{
   document.getElementById('CookieBox').value=document.cookie.replace(/;/g,';\n\n');
}

function ClearCookies() //Clear all the cookies on the current website
{
   var CookieNames=[]; //Remember the cookie names as we erase them for later output
   var MyCookies=document.cookie; //Remember the original cookie string since it will be changing soon
   var StartAt=0; //The current string pointer in MyCookies
   do //Loop through all cookies
   {
      var CookieName=MyCookies.substring(StartAt, MyCookies.indexOf('=', StartAt)).replace(/^ /,''); //Get the next cookie name in the list, and strip off leading white space
      CookieNames.push(CookieName); //Remember the cookie name
      document.cookie=CookieName+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"; //Erase the cookie
      StartAt=MyCookies.indexOf(';', StartAt)+1; //Move the string pointer to the end of the current cookie
   } while(StartAt!=0)
   document.getElementById('CookieBox').value='Clearing: '+CookieNames.join("\nClearing: "); //Output the erased cookie names
}
</script>

Live Example:
Logged