在Javascript中删除Cookie不一致 [英] Deleting a cookie in Javascript does not work consistently

查看:115
本文介绍了在Javascript中删除Cookie不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码设置,获取和删除cookie:

I have the following code to set, get, and delete cookies:

function set_cookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );

  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function get_cookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) &&
     ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}

function delete_cookie(name) {
    document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}



在我的程序中,我有一个称为活动消息的东西,记住活动消息的路径和存在。自然,我有一个丢弃邮件功能,将删除cookie。它看起来像这样:

In my program, I have something called an active message and use a cookie to remember the path and existence of the active message. Naturally I have a discard message function that will delete the cookie. It looks like this:

function discard_message() {
    alert('cookie = '+get_cookie('active_message'));
    clear_active_message_cookie();
    alert('should be null = '+get_cookie('active_message'));
    update_message('Discard', false, false);    
}

function clear_active_message_cookie(){
    delete_cookie("active_message");
}

正如你所看到的,删除后。奇怪的是,在我的应用程序的一部分,cookie删除成功,但丢弃在我的应用程序的另一部分的消息不工作。第二个Cookie警报打印Cookie的值。我已经确认该cookie的名称是相同的。

As you can see I have put alerts in to check if the cookie is readable after deleting it. The odd thing is that in one part of my application the cookie deletes successfully, but discarding a message in another part of my application does not work. The second cookie alert prints the value of the cookie. I have confirmed the name of the cookie is the same.

这几乎像我的请求删除一个cookie被拒绝。有谁知道在什么情况下这是可能的?

It is almost like my request to delete a cookie is getting denied. Does anyone know in what circumstances this is possible?

谢谢!

推荐答案

p>我认为删除的cookie只有在您离开当前页面后才会被停用。

I think a deleted cookie will only be de-actived after you have navigated away from the current page.

删除Cookie后,请在右下方插入代码;

Insert the code below right after you delete the cookie;

location.href =bringmeback.html ;

location.href="bringmeback.html";

bringmeback.html (重定向到当前页面);

bringmeback.html (redirect to current page);

location.href="currentpage.html";

您可以使用此代码测试您的Cookie是否已完全删除。

You can use this code to test if your cookie was completely deleted.

这篇关于在Javascript中删除Cookie不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆