设置文档位置href会阻止设置cookie

Setting document location href prevents setting cookie

本文关键字:设置 cookie href 文档 位置      更新时间:2023-09-26

当用户点击某个链接以在下一页中提供"返回"链接时,我想将当前页面记录在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');
 });