0

I know this i a duplicate from Clearing all cookies with JavaScript

but i still got a problem. I didn't add a path or a domain to my cookie but if i want to delete them by pressing the logout button the cookie seems untouched

Here i built the cookie and x is the response from a AJAX-Request witch gives me the informations i need:

function setCookie(x) {
      document.cookie = x;
}

but if i want to delete them in the same javascript-script it seems untouched and i don't get any errors This is the onLoad-function witch gets called when the user logout

function onLoad() {
        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";
            alert(document.cookie);
        }
    }

can anyone tell my why it dosent work. should i add a domain or a path even when i dont add one when i creates the cookie?

DoFlamingo
  • 232
  • 1
  • 18
  • Just a side note: It sounds like you're using cookies when you shouldn't be using cookies. If you're returning the information you want to store from an ajax request and having to save the cookie via JavaScript code, that doesn't sound like a use case for cookies. Unless you really want the browser to send the cookie -- *as a cookie* -- to the server with every single HTTP request (images, stylesheets, etc.), don't store the information in a cookie. Use [web storage](http://www.w3.org/TR/webstorage/) instead. It also has a sane API, unlike cookies. :-) – T.J. Crowder Nov 29 '19 at 15:04
  • Keep in mind you will only be able to delete cookies that are not marked as HttpOnly. – Taplar Nov 29 '19 at 15:13

0 Answers0