基本的cookie问题
对于某个项目,我希望在用户首次访问页面时出现一个警报/通知框,然后假设用户点击“x”关闭通知,它将在后续访问中停止出现 - - 我想这需要设置 cookie(通过 PHP),并且可能包含警报 div 通过 jQuery 响应 cookie 的存在或不存在?非常感谢这里关于如何开始的任何基本指导......
for a certain project I'd like to have an alert/notification box appear upon a user's initial visit to a page, and then assuming the user hits the "x" to close the notification, it will then cease to appear on subsequent visits-- I'd imagine this requires the setting of a cookie (via PHP), and perhaps the containing alert div responding to the presence or absence of the cookie via jQuery? any basic direction here in how to get started is much appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用浏览器端 cookie 来执行此操作:查看 jquery cookie 插件 http://plugins.jquery.com /项目/Cookie。
编辑:这就是我使用它的方式。
保存有用的通知会保留我的 helpfulnotices 对象,因此我不会显示任何超出应显示范围的有用通知。
下面是使用 jquery JSON cookie 扩展保存结构的代码:
在页面加载时我重新水化它:
You can do this using browser-side cookies: look at the jquery cookie plugin http://plugins.jquery.com/project/Cookie.
EDIT: here's how I'm using it.
Save helpful notices preserves my helpfulnotices object, so I don't show any one helpful notice more than it should be shown.
Here's the code to save the structure using the jquery JSON cookie extension:
and on page load I re-hydrate it:
您应该能够使用 cookie 来设置它。但并不能保证用户下次不会看到。它也不能保证新用户会看到它(如果他们在共享计算机上)。
这些事情最好与“用户登录系统”结合使用,这样您就可以单独跟踪每个用户。然后,您可以将设置存储在数据库中,并在后续访问时为每个用户调用该设置。
You should be able to set this with a cookie. However, it will not guarantee that the user will not see it next time. It also won't guarantee that new users will see it (if they are on a shared computer).
These things are always best to do in conjunction with a "user login system" so you can track each user individually. Then you can store the setting in the database and recall the setting for each user on subsequent visits.
您可以将信息保存在 cookie 中,但这意味着您将始终从客户端到服务器来回发送此 cookie。
如果您想防止这种情况发生,您可以使用名为 localStorage 的新 HTML5 变量
http ://www.bing.com/search?q=html5+localStorage&src=ie9tr
所以基本上,如果浏览器上存在 localStorage,并且不使用 cookies 进行回退,您就可以使用它。这将是最佳解决方案。
You can save the information in a cookie, but that will imply that you will send this cookie back and forth all the time from your client to your server.
If you want to prevent this, you can use the new HTML5 variable called localStorage
http://www.bing.com/search?q=html5+localStorage&src=ie9tr
So basically, you can use localStorage if it exists on the browser, if it doesn't fallback with cookies. That would be the optimal solution.