Show div in second visit

I want the div #topmessage to drop on the second user visit on the site. Also, when the user clicks the #close button, the message will no longer be displayed. I think I should set a cookie and count it, but I don’t know how to do it. I have the following cookie plugin installed 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>

Refresh . The reason I want to do this is to click the div for users who visited the site once and are now returning to a different date. I just don't want to show the div the second time the site is loaded, but it should be at a different date or session. As far as I see, there is no way to access the date the cookie was created?

+3
4

:

<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/roberkules/NL9jd/

​​ cookie
2- cookie

+5

, - . cookie , - cookie ( /). , .

"show div", . , .

+1

( 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");
        });
+1
source

First you need to determine if a cookie exists. If a cookie exists, then you know that the user has already visited once. If the cookie does not exist, its first visit will create a cookie.

0
source

All Articles