4

I am using this JavaScript Code, but it will return only cookies of a particular page. I want to clean all the cookies of Browser

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";
    }
};
Satish Somani
  • 65
  • 1
  • 2
  • 6
  • 1
    look at this http://stackoverflow.com/questions/595228/how-can-i-delete-all-cookies-with-javascript?rq=1 – Arunprasanth K V Mar 02 '15 at 07:25
  • i already use that but its also returning same – Satish Somani Mar 02 '15 at 07:27
  • You can only remove cookies created by JavaScript - if a cookie was create by the server, then you cannot remove it through JavaScript.you need to know name, path and domain of a cookie then only you can reliably delete the cookie – Arunprasanth K V Mar 02 '15 at 07:27
  • I want to clean the cookies that is created by facebook and our web application. what is path here.. Domain is facebook.com name is name of cookie – Satish Somani Mar 02 '15 at 07:29
  • I think what you are trying to do is accessing cross domain cookie.Which is not possible due to security reason. How ever I will look forward to see the answer of this. – Abhisek Malakar Mar 02 '15 at 07:37
  • You cannot reach cookies of another domain by javascript. You sould delete them from the browser developer tools. – Ibrahim ULUDAG Mar 02 '15 at 07:39
  • FYI. You can't delete all cookies via javascript, You can't even delete server cookies via javascript. You can only delete particular page/domain page. You just need to use browser feature to delete the cookies for all pages. – frogcoder Mar 02 '15 at 07:42

1 Answers1

4

You cannot delete cookies via Javascript that come from other domains than the page you are currently on. This is a browser security feature. And, if a cookie is marked for a particular path, you can only access it from a page on that particular path (even from the same domain).

And, for cookies that are marked HttpOnly (e.g. server-side access only cookies), you can't even delete those for your own domain via javascript.

The only way to clear all cookies is for you (the user) to use the browser's user interface to delete the cookies or to configure your browser to automatically clear cookies when you close the browser.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    some of the site there facebook login option is available on that sites we use that option for login and after logout from facebook and particular site we are able to again login through facebook with different user.. how we can maintain this point – Satish Somani Mar 02 '15 at 08:42