第二次访问时显示 div
我想让 div #topmessage 在用户第二次访问该网站时向下滑动。此外,当用户单击#close 时,该消息将不会再次显示。我想我必须设置一个 cookie 并对其进行计数,但不知道如何进行。我将以下 cookie 插件与 jquery http://plugins.jquery.com 安装在一起/files/jquery.cookie.js.txt
<script type="text/javascript">
jQuery(function($){
$('#topmessage').delay(1000).slideDown(400);
$("#close").click(function(){
$("#topmessage").slideToggle(400);
});
});
</script>
<div id="topmessage">
<a href="#" class="close" id="close">Close</a>
Hi welcome back, if you have any questions please feel free to <a href="/contact">contact me</a>.
</div>
更新:我想这样做的原因是将 div 推送给曾经访问过该网站并现在在另一个日期返回的用户。我不只是想在网站第二次加载时显示 div,但它也必须在另一个日期或会话上。据我所知,没有办法访问 cookie 的创建日期?
I want to have the div #topmessage slide down on the users second visit to the site. Also when the user clicks the #close the message will not be shown again. I guess I have to set a cookie and count it but not sure how. I have the following cookie plugin installed together with jquery http://plugins.jquery.com/files/jquery.cookie.js.txt
<script type="text/javascript">
jQuery(function($){
$('#topmessage').delay(1000).slideDown(400);
$("#close").click(function(){
$("#topmessage").slideToggle(400);
});
});
</script>
<div id="topmessage">
<a href="#" class="close" id="close">Close</a>
Hi welcome back, if you have any questions please feel free to <a href="/contact">contact me</a>.
</div>
Update: The reason I want to do this is to push the div to users who have visited the site once and are now returning on another date. I don’t just want to show the div the second time the site is loaded, but it has to be on another date or session as well. As far as I can see there is no way to access the creation date of a cookie?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能(没有使用过 Jquery cookie 但正在查看文档...)...
Probably (haven't used Jquery cookie but looking at the doc...) do...
首先你需要检测cookie是否存在。
如果cookie存在,那么你就知道用户已经访问过一次。
如果cookie不存在,则为第一次访问,创建cookie。
First you need to detect if a cookie exists.
If the cookie exists, then you know the user has already visited once.
If the cookie does not exist, its the first visit, create the cookie.
这应该有效:
如果您想在每次访问时更新 cookie 中的日期:
工作演示位于:http:// jsfiddle.net/rorkules/NL9jd/
更新
添加了 cookie 的过期日期和路径
仅为会话添加了第二个 cookie
This should work:
If you want to update the date in the cookie at each visit:
Working demo at: http://jsfiddle.net/roberkules/NL9jd/
UPDATE
added expiration date and path to cookie
added 2nd cookie just for the session
我认为这样的东西应该存储在后端并由服务器控制。 Cookie 的问题是有人可以删除所有 Cookie(或通过不同的浏览器/计算机登录)。听起来像是与用户相关的数据库表上的属性。
服务器可以简单地设置一个标志来“显示 div”,视图可以处理这个问题。不确定您正在使用什么服务器端技术。
I would think something like this should be stored in the backend and controlled by the server. The problem with cookies is that someone could delete all of their cookies (or log in via a different browser/computer). Sounds like a property on a database table relating to the user.
The server could simple set a flag to "show div" and the view could take care of that. Not sure what sever side technology you are using.