如何使用 Fragment 为 URL 设置 jquery cookie?

发布于 2024-12-07 08:56:50 字数 1130 浏览 3 评论 0原文

我正在使用jquery cookie插件 https://github.com/carhartl/jquery-cookie

我看到了关于如何使用 Fragment 构建 URL 的参考: AJAX 应用程序可抓取

最终网址规则看起来像这样:(

localhost/site/search#!key__searchword&page=1
localhost/site/search#!key__searchword&page=2
localhost/site/search#!key__searchword&page=3

原始网址应该是这样的:localhost/site/search?_escaped_fragment_key=searchword&page=1

上面的每个页面都有一个按钮,我想检查:

  • 如果用户从未点击过,他/她可以点击,
  • 如果用户点击过,则再次添加类 voted 禁止点击。
  • 我想将 cookie 设置为 7 天。

我的 javascript 代码:

$(document).ready(function(){
  $(".up").live('click',function() {
    $.cookie('up', 'vote', { expires: 7, path: '/', domain: 'http://127.0.0.1/site/' });
    var up = $.cookie('up');
    if (up == 'vote') {
        $(".up").addClass('voted');
    };
  });
});

无论如何,jquery cookie 对我不起作用。如何使用 Fragment 设置 URL 的 jquery cookie?

I am using the jquery cookie plug-in https://github.com/carhartl/jquery-cookie.

I saw this reference on how to build a URL with Fragment:
AJAX Applications Crawlable

The final url rule looks like this:

localhost/site/search#!key__searchword&page=1
localhost/site/search#!key__searchword&page=2
localhost/site/search#!key__searchword&page=3

(original url should like this: localhost/site/search?_escaped_fragment_key=searchword&page=1)

Each above page has one button, I want to check:

  • If user never clicked, he/she can do the click,
  • If user has clicked, then add class voted for forbidden click again.
  • I want set cookie for 7 days.

My javascript code:

$(document).ready(function(){
  $(".up").live('click',function() {
    $.cookie('up', 'vote', { expires: 7, path: '/', domain: 'http://127.0.0.1/site/' });
    var up = $.cookie('up');
    if (up == 'vote') {
        $(".up").addClass('voted');
    };
  });
});

In anycase, the jquery cookie does not work for me. How can I set the jquery cookie for a URL with Fragment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-12-14 08:56:50

片段永远不会发送到服务器,因此您无法为片段设置 cookie。您只能为路径设置 cookie,并且根据 RFC,它默认为我认为的直到最后一个正斜杠的路径。

Fragments are never sent to the server, so you cannot set a cookie for a fragment. You can only set a cookie for the path, and it defaults to the path up to the last forward slash I believe according to the RFC.

我最亲爱的 2024-12-14 08:56:50

您应该在检查 cookie 是否存在之后设置 cookie 基本实现应如下所示:

$(document).ready(function(){
  $(".up").live('click',function() {
    var up = $.cookie('upvote');
    var currentLoc = location.hash;
    if (up && up.indexOf(currentLoc) == -1) {
        $(this).addClass('voted');
    };
    else {
        var saveCookie = up ? up + currentLoc : currentLoc;
        $.cookie('upvote', saveCookie, { expires: 7, path: '/', domain: 'http://127.0.0.1/site/' });
        //rest of functions
    }
  });
});

通过 location.hash 检索当前位置。如果 cookie 中已存在该位置的哈希值,则将执行Already voted 例程。否则,cookie 会被更新。

请注意,每次更新 cookie 时,此脚本都会阻止用户在 7 天内投票。如果您想为每个页面单独设置 7 天惩罚,您可以使用哈希值作为 cookie 名称。

You should set the cookie after checking whether the cookie exists or not A basic implementation should look like this:

$(document).ready(function(){
  $(".up").live('click',function() {
    var up = $.cookie('upvote');
    var currentLoc = location.hash;
    if (up && up.indexOf(currentLoc) == -1) {
        $(this).addClass('voted');
    };
    else {
        var saveCookie = up ? up + currentLoc : currentLoc;
        $.cookie('upvote', saveCookie, { expires: 7, path: '/', domain: 'http://127.0.0.1/site/' });
        //rest of functions
    }
  });
});

The current location is retrieved through location.hash. If the location's hash already exists in the cookie, the Already voted routine is followed. Else, a cookie is updated.

Note that this script will prevent a user from voting for seven days, each time the cookie is updated. You can use the hash as a cookie name if you want to set the seven day penalty individually for each page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文