设置文档位置 href 可防止设置 cookie

Setting document location href prevents setting cookie

我想在用户点击某些 link 以在下一页中提供 'return to' link 时将当前页面记录在 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?

添加 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');
 });