views:

1229

answers:

3

Good morning people

I am attempting to clear all of my cookies, some of which are set on a server and some of which are browser cookies.

I have tried using document.cookie = "" however this did not satisfy my needs so I wrote some Javascript code, which can be seen below

The pauseClearAllCookies function is called onload.

<script type="text/javascript">

     //var cookie_names = new Array("__utma", "__utmb", "__utmc", "__utmz", "mortgage", "track_source","currency","selenium_testing","visit_secure_token", "rdb_history", "_csuid", "search", "finance", "searchhistory");

     function pauseClearAllCookies(){
      Set_Cookie("selenium_testing","1");
      drawTable();
      setTimeout("checkCookies();",1000);
      document.getElementById('msg').innerHTML = "Gathering Cookies....";
     }

     var cookie_counter = 0;
     var cookieList;

     function checkCookies(){
      if(document.cookie.indexOf(";") != -1){
       cookieList = document.cookie.split(";");
      }else{
       cookieList = [document.cookie];
      }
      setTimeout("clearAllCookies();",1000);
     }

     function clearAllCookies(){

      document.getElementById('msg').innerHTML = "Deleting Cookies....";

      if(cookie_counter < cookieList.length ){

       var cookieName = "";

       if(cookieList[cookie_counter].indexOf("=") != -1){
        cookieName = cookieList[cookie_counter].split("=")[0];
       }else{
        cookieName = cookieList[cookie_counter];
       }
       document.getElementById('msg').innerHTML = "Deleting Cookie: "+cookieName;
       // clear js cookies
       Delete_Cookie(cookieName, '/', document.domain);
       Delete_Cookie(cookieName, '/', '.www.abc.co.uk');
       Delete_Cookie(cookieName, '/', 'www.abc.co.uk');
       Delete_Cookie(cookieName, '/', '.abc.co.uk');

       // clear server cookies
       Delete_Cookie(cookieName, '/', '');

       // increment counter
       cookie_counter++;

       drawTable();
       //recall the function
       setTimeout("clearAllCookies();",800);
      }else{
       Set_Cookie("selenium_testing","1");
      }
     }
     function drawTable() {
      var allcookies = document.cookie.split(";");
      document.getElementById('heading').innerHTML = allcookies.length + " cookie found";

      var table_html = "<table class='data'>";
      for(var i=0; i < allcookies.length; i++){
       var cookie = allcookies[i].split("=");
       table_html +=  "<tr>";
       table_html +=  "<td>"+(i+1)+"</td>";
       table_html +=  "<td>" + cookie[0] + "</td>";
       table_html +=  "<td>" + cookie[1] + "</td>";
       table_html +=  "<tr>";
      }

      table_html += "</table>";

      document.getElementById('table').innerHTML = table_html;
     }
    </script>

The problem is that every time I run this code one cookie is left behind, it is looped through however not deleted and its ALWAYS item number one in the Array. Any help in sorting this issues really would be very much appreciated

Thank you all for your time

cameron

+2  A: 

have you tried to set all the cookie time formats to "a millisecond ago"? that ought to clear it up.

BerggreenDK
Did this fix your problem cameron? please mark the correct answer, if you found it so the community can grow which correct answers.
BerggreenDK
A: 

Many apologies I forgot to mention that I have all ready got this piece of code in another file which is called before this one.

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

The name path and domain are all valid

cameron
A: 

have you checked the examples on this page: http://www.w3schools.com/JS/js_cookies.asp

They might do the trick...?

BerggreenDK