Jquery Click Popup and cookies (Show popup once)

2019-02-27 18:39发布

问题:

Load Jquery

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

I'm trying to get a popup to only load once then not show again for another 31 days.

    <script type="text/javascript"><!--
    jQuery(document).ready(function()
    {
if (jQuery.cookie('test_status') != '1')
{
    setTimeout(function()
    {
        jQuery('.lightbox-handle').reveal()
        jQuery('.lightbox-handle').click();
        jQuery.cookie('test_status', '1', { expires: 31}); 
    }, 1000);
}
    });
    //--></script>

回答1:

Are you including the jQuery cookie plugin, https://github.com/carhartl/jquery-cookie. You can find on CDNJS at http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.min.js

<script type="text/javascript"
        src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript"
        src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.min.js"></script>
<script type="text/javascript">
    $(function() {
        if ($.cookie('test_status') != '1')
        {
            setTimeout(function()
            {
                alert('foo');
                jQuery.cookie('test_status', '1', { expires: 31}); 
            }, 1000);
        }
    }
</script>

Works for this fiddle http://jsfiddle.net/WyYZ8/



回答2:

I had the same problem and I found this solution:

<script type="text/javascript">
var x = document.cookie;
    if (x =="")
      {
                var url = window.location;
            if (url!="http://localhost/test/jquery-popup.html")
                {
                $(document).ready(function(){
                    setTimeout(function() {
                        alert( "the current url is "+url+ "");
                        //$.fn.colorbox({html:'<div style="width:301px;height:194px;"><a href="http://livebook.in/"><img src="res/homer.jpg" style="width:301px;height:194px;" alt="The linked image" /></a></div>', open:true});
                    }, 500);
                });
                } 
            else 
                {
                $(document).ready(function(){
                setTimeout(function() {
                    //alert( "else");
                    $.fn.colorbox({html:'<div style="width:301px;height:194px;"><a href="http://livebook.in/"><img src="res/homer.jpg" style="width:301px;height:194px;" alt="The linked image" /></a></div>', open:true});
                }, 500);
                setTimeout(function(){$.fn.colorbox.close()},3000);
                //document.cookie="apparsoSI";
                    var d = new Date();
                    d.setTime(d.getTime()+(30*24*60*60*1000)); //expire in 30 days
                    var expires = "expires="+d.toGMTString();
                    document.cookie = "apparsoYES" + "=" + "YES" + "; " + expires;
                    });
                }
     }

    else
        {
        alert( "cookie ok");

        }

</script>