Nirjhar Vermani
Nirjhar Vermani

Reputation: 1245

Create and Delete cookies for external websites on their subdomain

Set Cookie function in Jquery

function setCookie(cookieName, cookieValue, cookieExpireDays) {
        var d = new Date();
        d.setTime(d.getTime() + (cookieExpireDays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";" + "path=/";
    }

Delete function in Jquery

function deleteCookie(name) {
    var domain = location.hostname,
        path = '/'; // root path

    document.cookie = [
        name, '=',
        '; expires=' + new Date(0).toUTCString(),
        '; path=' + path,
        '; domain=' + domain
    ].join('');
}

This works fine for local links, but when I try to use it on external websites. I can get my document.cookie but deleteCookie function does not delete the cookie. Any ideas?

*Please remember, I am just running these scripts from console of Google Chrome

Upvotes: 1

Views: 1648

Answers (3)

Nirjhar Vermani
Nirjhar Vermani

Reputation: 1245

Alright, I am answering my own question because I changed few things and it works now.

    var domain = location.hostname

    function setCookie(cookiename, cookievalue, expiredays, domain) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expiredays=" + date.toGMTString();   
            document.cookie = cookiename+ "=" + cookievalue+ expires + "; domain=" + domain + "; path=/";
    }

function deleteCookie(cookiename, domain) {
    setCookie(cookiename, "", -1, domain);
}

deleteCookie("Cookie_Name",domain)

Upvotes: 0

John Wu
John Wu

Reputation: 52290

If the cookies are HttpOnly, you will not be able to delete them using javascript or jquery.

Make sure your domain and path match exactly. If they are different (e.g. .subdomain.domain.com instead of .domain.com, or /path instead of /) then the script will not affect the cookies.

Upvotes: 0

justDan
justDan

Reputation: 2333

I might be misunderstanding something in your question, but here's how I handled the getting, setting, and deleting of a cookie I set.

fiddle: https://jsfiddle.net/hmvyu3L6/

<button class='set'>set</button>
<button class='get'>get</button>
<button class='delete'>delete</button>

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = 'expires=' + d.toUTCString();
    document.cookie = cname + '=' + cvalue + '; ' + expires;
  }

  function getCookie(cname) {
    var name = cname + '=',
    ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length,c.length);
      }
    }
    return '';
  }

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

  var value = 'hey there';

  $('.set').on('click', function() {
    setCookie('testCookie', value, 360);
  });

  $('.get').on('click', function() {
    console.log(getCookie('testCookie'));
  });

  $('.delete').on('click', function() {
    deleteCookie('testCookie');
  });

Upvotes: 1

Related Questions