如何使用javascript删除浏览器cookie?

时间:2021-10-06 14:30:13

How can I delete the session information from my browser by using javascript? Is it possible to do?

如何使用javascript从浏览器中删除会话信息?有可能吗?

4 个解决方案

#1


1  

Session information is usually stored on the server. An HTTP request to a page that destroys the session would normally do the trick (using AJAX if you wish).

会话信息通常存储在服务器上。对破坏会话的页面的HTTP请求通常会起作用(如果您愿意,可以使用AJAX)。

For cookies you can set the cookie expiry date to the current date, this will expire the cookie and remove it.

对于cookie,您可以将cookie到期日期设置为当前日期,这将使cookie过期并将其删除。

var d = new Date();
document.cookie = "cookiename=1;expires=" + d.toGMTString() + ";" + ";";

#2


1  

Basically all that you need is to set cookie's expiry date to some date in the past.

基本上你需要的是将cookie的到期日期设置为过去的某个日期。

var cookie_date = new Date ( );  // now
cookie_date.setTime ( cookie_date.getTime() - 1 ); // one second before now.
// empty cookie's value and set the expiry date to a time in the past.
document.cookie = "logged_in=;
                  expires=" + cookie_date.toGMTString();

Click here or here for more informations.

单击此处或此处获取更多信息。

#3


0  

If you don't set an expiry date to a cookie, by definition it is only session lived. Ie. it will be deleted when the user will close his browser. Thus, no need for clean up.

如果您没有为cookie设置到期日期,根据定义它只是会话生效。 IE浏览器。当用户关闭浏览器时,它将被删除。因此,无需清理。

#4


0  

In addition to the expiration we have been writing a value "deleted" or similar to the cookie. In some cases we've discovered that the cookies don't expire immediately and accessing them from js might give false results for a short while.

除了到期之外,我们一直在写一个值“已删除”或类似于cookie。在某些情况下,我们发现cookie不会立即过期,并且从js访问它们可能会在短时间内给出错误的结果。

#1


1  

Session information is usually stored on the server. An HTTP request to a page that destroys the session would normally do the trick (using AJAX if you wish).

会话信息通常存储在服务器上。对破坏会话的页面的HTTP请求通常会起作用(如果您愿意,可以使用AJAX)。

For cookies you can set the cookie expiry date to the current date, this will expire the cookie and remove it.

对于cookie,您可以将cookie到期日期设置为当前日期,这将使cookie过期并将其删除。

var d = new Date();
document.cookie = "cookiename=1;expires=" + d.toGMTString() + ";" + ";";

#2


1  

Basically all that you need is to set cookie's expiry date to some date in the past.

基本上你需要的是将cookie的到期日期设置为过去的某个日期。

var cookie_date = new Date ( );  // now
cookie_date.setTime ( cookie_date.getTime() - 1 ); // one second before now.
// empty cookie's value and set the expiry date to a time in the past.
document.cookie = "logged_in=;
                  expires=" + cookie_date.toGMTString();

Click here or here for more informations.

单击此处或此处获取更多信息。

#3


0  

If you don't set an expiry date to a cookie, by definition it is only session lived. Ie. it will be deleted when the user will close his browser. Thus, no need for clean up.

如果您没有为cookie设置到期日期,根据定义它只是会话生效。 IE浏览器。当用户关闭浏览器时,它将被删除。因此,无需清理。

#4


0  

In addition to the expiration we have been writing a value "deleted" or similar to the cookie. In some cases we've discovered that the cookies don't expire immediately and accessing them from js might give false results for a short while.

除了到期之外,我们一直在写一个值“已删除”或类似于cookie。在某些情况下,我们发现cookie不会立即过期,并且从js访问它们可能会在短时间内给出错误的结果。