第二次访问时显示 div

发布于 2024-11-09 22:30:54 字数 866 浏览 0 评论 0原文

我想让 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 技术交流群。

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

发布评论

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

评论(4

想挽留 2024-11-16 22:30:57

可能(没有使用过 Jquery cookie 但正在查看文档...)...

jQuery(function($){ 
    if($.cookie("showMessage") == "true") {
    $('#topmessage').delay(1000).slideDown(400);
$.cookie("showMessage","never");
}
else if($.cookie("showMessage") != "never"){
$.cookie("showMessage","true");
}
    $("#close").click(function(){
          $("#topmessage").slideToggle(400);
$.cookie("showMessage","never");
        });

Probably (haven't used Jquery cookie but looking at the doc...) do...

jQuery(function($){ 
    if($.cookie("showMessage") == "true") {
    $('#topmessage').delay(1000).slideDown(400);
$.cookie("showMessage","never");
}
else if($.cookie("showMessage") != "never"){
$.cookie("showMessage","true");
}
    $("#close").click(function(){
          $("#topmessage").slideToggle(400);
$.cookie("showMessage","never");
        });
去了角落 2024-11-16 22:30:57

首先你需要检测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.

她说她爱他 2024-11-16 22:30:56

这应该有效:

<script type="text/javascript">
    $(function(){
        var value = $.cookie('the_cookie'),
            date = null;
        if (/^\d+$/.test(value || "")) {
             date = new Date();
             date.setTime(value);
        } else if ((value || "") == "") {
             $.cookie('the_cookie', new Date().getTime());
        }

        if (null != date /* && date meets your requirements */){
            $('#topmessage').delay(1000).slideDown(400);
            $("#close").click(function(){
                $("#topmessage").slideToggle(400);
            });
        }
    });
</script>

如果您想在每次访问时更新 cookie 中的日期:

        if (/^\d+$/.test(value || "")) {
             date = new Date();
             date.setTime(value);
        }
        $.cookie('the_cookie', new Date().getTime());

工作演示位于:http:// jsfiddle.net/rorkules/NL9jd/

更新

添加了 cookie 的过期日期和路径
仅为会话添加了第二个 cookie

This should work:

<script type="text/javascript">
    $(function(){
        var value = $.cookie('the_cookie'),
            date = null;
        if (/^\d+$/.test(value || "")) {
             date = new Date();
             date.setTime(value);
        } else if ((value || "") == "") {
             $.cookie('the_cookie', new Date().getTime());
        }

        if (null != date /* && date meets your requirements */){
            $('#topmessage').delay(1000).slideDown(400);
            $("#close").click(function(){
                $("#topmessage").slideToggle(400);
            });
        }
    });
</script>

If you want to update the date in the cookie at each visit:

        if (/^\d+$/.test(value || "")) {
             date = new Date();
             date.setTime(value);
        }
        $.cookie('the_cookie', new Date().getTime());

Working demo at: http://jsfiddle.net/roberkules/NL9jd/

UPDATE

added expiration date and path to cookie
added 2nd cookie just for the session

送舟行 2024-11-16 22:30:56

我认为这样的东西应该存储在后端并由服务器控制。 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.

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