如何使用 Fragment 为 URL 设置 jquery cookie?
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
片段永远不会发送到服务器,因此您无法为片段设置 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.
您应该在检查 cookie 是否存在之后设置 cookie 基本实现应如下所示:
通过
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:
The current location is retrieved through
location.hash
. If the location's hash already exists in the cookie, theAlready 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.