【问题标题】:Setting document location href prevents setting cookie设置文档位置 href 可防止设置 cookie
【发布时间】:2015-03-21 12:08:03
【问题描述】:

当用户单击某个链接以在下一页中提供“返回”链接时,我想将当前页面记录在 cookie 中。顺便说一句,我不在 asp.net 中使用页面 Request.UrlReferrer,因为用户可能会在打开的新页面中进行分页,但推荐 URL 不得更改。这是我的代码:

$('.options a').click(function (e) {
    var a = $(this); 
    $.cookie('return-to', a.attr('href') + '#' + a.closest('.names').attr('id'), {expires:1});
   e.preventDefault();
   document.location.href = a.attr('href');
 });

上面的代码没有设置cookie,但是这个设置了:

$('.options a').click(function (e) {
        var a = $(this); 
        $.cookie('return-to', a.attr('href') + '#' + a.closest('.names').attr('id'), {expires:1});
       e.preventDefault();
     //  document.location.href = a.attr('href');
     });

为什么设置 document.location.href 会阻止设置 cookie?

【问题讨论】:

    标签: javascript jquery asp.net cookies


    【解决方案1】:

    添加 cookie 选项的路径解决了这个问题。但我仍然不明白,因为新页面中的域没有改变

    $('.options a').click(function (e) {
        var a = $(this); 
        $.cookie('return-to', a.attr('href') + '#' + a.closest('.names').attr('id'), {expires:1, path:'/'});
       e.preventDefault();
       document.location.href = a.attr('href');
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多