jQuery set cookie for first visit

后端 未结 2 390
遇见更好的自我
遇见更好的自我 2021-02-03 15:53

I got a script like that (I use jQuery cookie js to set cookies) to display layer on first visit.



        
相关标签:
2条回答
  • 2021-02-03 16:13

    Because you are creating the cookie, it will never be null. You need to change your logic to first check if the cookie exists. If not, show the .newsletter_layer element, then set the cookie value:

    <script type="text/javascript">
        $(document).ready(function() {
            // check cookie
            var visited = $.cookie("visited")
    
            if (visited == null) {
                $('.newsletter_layer').show();
                alert($.cookie("visited"));         
            }
    
            // set cookie
            $.cookie('visited', 'yes', { expires: 1, path: '/' });
        });
    </script>
    
    0 讨论(0)
  • 2021-02-03 16:20

    If somebody stumbles across this 6 years into the future like I, I got this to work with modification:

    Replace: $.cookie -> Cookies

    0 讨论(0)
提交回复
热议问题