JS cannot clear the cookie solution

Source: Internet
Author: User
Tags setcookie

JS cannot clear the cookie solution

When you use Sdmenu.js in your project, you need to clear the cookie when you log on, and Sdmenu will save the cookie by default.

Here's how sdmenu.js saves cookies

Document.cookie = "Sdmenu_" + encodeuricomponent (this.menu.id) + "=" + States.join ("") + "; Expires= "+ d.togmtstring () +"; path=/";

Here's what I'm looking for on the internet. The way to clear cookies is always impossible to delete cookies

1. Pass the cookie name

function Deletecookie (name) {
var date=new date ();
Date.settime (Date.gettime ()-10000);
Document.cookie=name+ "=V; Expire= "+date.togmtstring () +"; path=/";

2. Circular deletion of all cookies

JS Traversal All Cookies
function foreach ()
{
var Strcookie=document.cookie;
var arrcookie=strcookie.split (";"); Cutting multi-Cookie into multiple name/value pairs
for (var i=0;i <arrcookie.length;i++)
{//Traversal cookie array, processing each cookie pair
var arr=arrcookie[i].split ("=");
if (arr.length>0)
Delcookie (Arr[0]);
}

}
function Getcooki (offset)

{
var endstr = document.cookie.indexOf (";", offset);
if (endstr = =-1)
Endstr = Document.cookie.length;
Return decodeURIComponent (document.cookie.substring (offset, endstr));
}
function Delcookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = GetCookie (name);
Document.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();
}

function GetCookie (name)
{
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Document.cookie.substring (i, j) = = arg)
Return Getcooki (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
return null;
}

This web site was later found to explain why cookies could not be deleted.

http://php.js.cn/blog/delete-cookie/

There is a problem with a cookie today: a cookie named username, whose domain is www.umiwi.com, cannot be deleted, causing the user to log out later. After a long day finally found the problem: it is my basic knowledge of the cookie is not clear ....

Cookies do not only have names and values of two attributes, but also domains (domain), expiration time (expires), path, and so on. A cookie with the same name can exist in different domains and different paths. Like this page: Poke me with the mouse

In general, we delete cookies by using the same name and expiration time as a cookie covering the past time. Be sure to figure out the domain and path of the cookie you want to delete, and the cookie domain and path must be the same to be overwritten . Otherwise the effect is that the cookie that wants to be deleted has magical vitality and cannot be erased ~ ~ ~

In addition, I found that when setting a cookie, if no domain name is specified, the domain of this cookie is assumed to be the current domain, such aswww.umiwi.com。 If you specify a domain when you set it, the browser will automatically add the.。 For example, PHP code:setcookie (' Test ', ' a ', 0, ' www.umiwi.com ');Then the domain of cookies stored in the browser is. www.umiwi.com。So, the best way to do this is to set the cookie, how to write it, and how to write it when you delete it. Or there will be the situation I encountered: clearly browser see Username=longbill, domain forwww.umiwi.comCookie, but it's written in PHP.Setcookie (' username ', ', Time () -1000, ' www.umiwi.com ', '/');The cookie cannot be deleted. The reason is that I deleted the cookie operation is actually sent a new name username, the value is empty, the expiration time is the past 1000 seconds, the domain is. www.umiwi.com, and the path is/cookie. This cookie is immediately expired in the browser and cannot be seen. And I'm going to delete that inhttp://www.umiwi.com/The cookie is alive and well ...

There is no add path (path) in the code that deleted the cookie.

Original:

Document.cookie = "Sdmenu_" + encodeuricomponent (this.menu.id) + "=" + States.join ("") + "; Expires= "+ d.togmtstring () +"; path=/";

New:

var date=new date ();
Date.settime (Date.gettime ()-10000);
Document.cookie=name+ "=V; Expire= "+date.togmtstring() +"; path=/";

In addition to the path, there may be other situations such as domains, insurance is how to set the cookie style, what should be deleted when the style, plus the Expire property on the line


JS cannot clear the cookie solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.