在第一次页面加载时设置 cookie
如何仅在第一次加载页面时通过 jQuery cookie ($.cookie()
) 库设置 cookie?我目前有一个切换功能,可以在点击事件时更改相关 cookie 的值,
$('.family-filter').click(function() {
if ($.cookie('ff') == null) {
$.cookie('ff', 'on', {
expires: 1,
path: '/'
});
} else {
$.cookie('ff', null, {
path: '/'
});
}
});
但如果尚未设置,我需要一个默认值。
也很高兴通过 PHP 设置它,但在这种情况下我更喜欢通过 Javascript 来设置
How can I set a cookie via jQuery cookie ($.cookie()
) library, only when the page is loaded for the first time? I currently have a toggle feature, to change the value of the cookie in question upon a click
event
$('.family-filter').click(function() {
if ($.cookie('ff') == null) {
$.cookie('ff', 'on', {
expires: 1,
path: '/'
});
} else {
$.cookie('ff', null, {
path: '/'
});
}
});
But I need a default value, if none is set already.
Also happy to set it via PHP, but I'd prefer for this case, to do it via Javascript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我像这样添加一个 cokkie:
它对我来说效果很好,如果您需要在用户第一次访问网站时运行某些内容,您可以在
if
子句中添加更多代码。I'm adding a cokkie like so:
It works fine for me, and you can add some more code inside the
if
clause if you need something to run while the user it's on his first visit on the website.在页面加载时您可以设置此代码来检查和设置cookie
In page load you can set this code to check and set cookies
好吧,我决定这样实现:
Well, I decided to implement it this way: