Solution for JS failure to clear cookies

Source: Internet
Author: User

Solution for JS failure to clear cookies

When sdmenu. js is used in the project, the Cookie needs to be cleared during login, while sdmenu saves the Cookie by default.

The following describes how to save the Cookie in sdmenu. js:

Document. cookie = "sdmenu _" + encodeURIComponent (this. menu. id) + "=" + states. join ("") + "; expires =" + d. toGMTString () + "; path = /";

The following method is used to clear cookies on the Internet.

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. Delete all cookies cyclically

// Js traverses all cookies

Function foreach ()

{

Var strCookie = document. cookie;

Var arrCookie = strCookie. split (";"); // cut multiple cookies into multiple name/value pairs

For (var I = 0; I

{// Traverse the cookie array to process 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;

}

Later I found a netizen's blog explaining why the Cookie cannot be deleted.

The original blog is as follows:

Today, I encountered a cookie problem: the cookie named username with the domain name www.45it.net cannot be deleted, and the user cannot exit after login. After a long time, I finally found out the problem: I did not know the basic knowledge about cookies ....

Cookies not only have attributes such as name and value, domain, expiration time (expires), and path. Cookie with the same name may exist in different domains and different paths. For example, this page: use the mouse to stamp me

In general, the method for deleting a cookie is to overwrite the Cookie with the same name and expiration time. At this time, you must understand the domain and path of the cookie you want to delete. The Cookie domain and path must be the same to be overwritten. Otherwise, the result is that the Cookie to be deleted has magical vitality and cannot be cleared ~~~

In addition, I found that when setting a Cookie, if no domain name is specified, the cookie domain is set to the current domain by default, such as www.45it.net. If a domain is specified during the setting, the browser will automatically add the domain before it is saved .. For example, PHP code: setcookie ('test', 'A', 0, 'www .45it.net '); then the domain of the Cookie stored in the browser is .www.45it.net. Therefore, the best way is to set the Cookie and write it when you delete it. Otherwise, I may encounter a situation: Clearly there is a Cookie in the browser with username = longbill, and the domain is www.45it.net, but setcookie ('username ','', time ()-1000, 'www .45it.net ','/'); the Cookie cannot be deleted. The reason is that my Cookie deletion operation actually sent a new Cookie named username with a blank value, expiration time of the past 1000 seconds, domain of .www.45it.net, and path. The Cookie will expire immediately after it is sent to the browser, and nothing will be seen. The cookieon http://www.45it.net/ I want to delete is a good example...

It turns out that no path is added in the Cookie deletion code)

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 the domain. The insurance method is to set the Cookie style, the style to be deleted, and the expire attribute.

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.