0

我有三个cookie compare1、compare2 和compare3。用户单击关闭按钮后,我想删除 cookie,然后获取设置为 compare2 或 compare3 或 compare1 的 cookie。我也在使用 javascript,但我的 cookie 没有被删除。这是我的代码:

  $('#srchresult #frmCompare a.close').click( function() {
   var parentName = $(this).parent().attr('id');
   var parentId=  parentName.replace('dvPkg','');       
   document.cookie = 'compare'+parentId + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        alert(document.cookie);
        var username1=getCookie("compare1");
        var username2=getCookie("compare2");
        var username3=getCookie("compare3");

        if (username1!=null && username1!="")
  {
  alert(username1);
  }
   if (username2!=null && username2!="")
  {
  alert(username2);
  }
   if (username3!=null && username3!="")
  {
  alert(username3);
  }
});

尽管删除了 cookie,但我仍然在警报中收到 cookie。我想在不刷新页面的情况下删除 cookie 并获取剩余的两个 cookie。

创建 cookie 时,我没有设置域或路径。我想在不刷新页面的情况下删除 cookie。我正在使用 php 创建 cookie

  $cookie = array( 'name' => 'compare' . $noOfItem, 'value' => $noOfItem . '^' . $postId . '^' . $postType . '^' . $return_result['deal_title'] . '^' . $return_result['img_filename'] . '^' . $postcompareCountry, 'expire' => '0', ); 

$this->input->set_cookie($cookie);
4

1 回答 1

0

您需要为您的 cookie 添加一个“路径”。例如:

document.cookie = 'cookie2=yet another test; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';

路径代表您网站中 cookie 可读的相对路径。path=/ 意味着它将在您的整个网站中可读。path=/common/ 意味着它只能在 /common/ 文件夹(及其子文件夹)中读取

这是关于 cookie的MDN上很好的教程:-

更新 :-

在创建 Cookie 时尝试添加路径:-

 $cookie = array( 'name' => 'compare' . $noOfItem, 'value' => $noOfItem . '^' . $postId . '^' . $postType . '^' . $return_result['deal_title'] . '^' . $return_result['img_filename'] . '^' . $postcompareCountry, 'expire' => '0','path' => '/' ); 

你也可以看看这个:-

于 2012-11-09T05:57:49.437 回答