MantheBean
MantheBean

Reputation: 1

Deleting cookies from particular site using javascript not working

Basically what I'm trying to do is: if you're on example.com, clear all the cookies. (using a chrome extension, I've put the background.js file in here). I can't see how this isn't working.

onload = function () {

//alert("test2");
//if we're on example.com, change the referrer header
if(currentUrl.indexOf("example.") !== -1) {
    alert("it's in here");
        function deleteAllCookies() {
            var cookies = document.cookie.split(";");

            for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";

    }

}       

}

Upvotes: 0

Views: 79

Answers (2)

Marko
Marko

Reputation: 2734

Change your code to the following, this way your function will be called.

onload = function () {

//alert("test2");
//if we're on example.com, change the referrer header
if(currentUrl.indexOf("example.") !== -1) {
    alert("it's in here");
    var cookies = document.cookie.split(";");

     for (var i = 0; i < cookies.length; i++) {
       var cookie = cookies[i];
       var eqPos = cookie.indexOf("=");
       var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
       document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
     }       
}

Upvotes: 0

Stephan Bijzitter
Stephan Bijzitter

Reputation: 4595

You create the deleteAllCookies function, but do not execute it. Simply call it (or do not create a function but just execute the code).

Upvotes: 1

Related Questions