JS can not clear the cookie solution

Source: Internet
Author: User
Tags join php code php write setcookie split

When using Sdmenu.js in a project, you need to clear cookies at logon, and Sdmenu will save cookies by default

Here's how to save cookies sdmenu.js

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

The following is the way I found on the internet to clean cookies, and I can't delete cookies at all times.

1. Pass Cookie Name

function Deletecookie (name) {

var date=new date ();

Date.settime (Date.gettime ()-10000);

Document.cookie=name+ "=V; Expire= "+date.togmtstring () +"; path=/";

2, cycle Delete all Cookies

JS Traverse All Cookies

function foreach ()

{

var Strcookie=document.cookie;

var arrcookie=strcookie.split (";"); Cut multiple cookies to multiple name/value pairs

for (Var i=0;i

{//Traversal cookie array, processing each cookie to

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 found a netizen's blog, introduced the reason for the inability to delete cookies

The original blog is as follows:

A cookie problem was encountered today: the cookie named username, domain www.45it.net, cannot be deleted, causing the user to log on and not exit. After a long time finally found the problem: the original is my basic knowledge of cookies did not understand ....

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

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

In addition, I found that if you do not specify a domain name when setting cookies, the domain of the cookie that is set is the current domain, such as www.45it.net. If a domain is specified when it is set up, the browser will automatically precede it when 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 how to write, delete the time how to write. Or there will be a situation I encountered: clearly in the browser to see Username=longbill, domain for www.45it.net cookies, but in PHP write Setcookie (' username ', ', Time ()-1000, ' Www.45it.net ', '/'); The cookie could not be deleted. The reason is that my deletion of cookies is actually a new name named Username, the value is empty, the expiration time is past 1000 seconds, the domain is. www.45it.net, the path is/the cookie. This cookie expires in the browser and does not see anything. And I want to delete the cookie on the http://www.45it.net/is alive and well ...

There is no path in the code where the cookie was removed (path)

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 (path), there may be a domain, and other circumstances, insurance is the way to set the cookie is what style, delete should be what style, plus the Expire property on the line

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.