Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a jQuery cookie to show popup only once

I'm an absolute newbie in jQuery. I'm learning, but there is a Christmas message that I need to get up and running within no time.

I've included these in the header of the page:

<script type="text/javascript" src="scripts/jquery-1.7.min.js"></script>
<script type="text/javascript" src="scripts/jquery.cookies.2.2.0.min.js"></script>` 

Then follows the message using a jQuery popup. Here it is:

<script type="text/javascript">
$(document).ready(function() {  
        var id = '#dialog';

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.7);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2-220);

        //transition effect
        $(id).fadeIn(2000);     

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });     

});

</script>

In the body I've put the message:

<div style="top: 199.5px; left: 200px; display: none;" id="dialog" class="window">  
XMAS MESSAGE
<a href="#" class="close">Shut this popup.</a>
</div>

So far so good. The next step would be not to bore my returning visitors with the same message over and over (postpone for sixty days would be good enough).

So I want to set a cookie using the jQuery cookie plug-in:

function setCookie() {
    $.cookie('test_status', '1', { path: '/', expires: 60 });
    return false;
}

Which is then found the next time a visitor hits the same page and the Christmas message is not shown till the message expires.

Now if-else statements are the higher kind of jQuery I'm not familiar with just yet. So, could anyone explain it to me?

like image 950
Peter Amsterdam Avatar asked Dec 06 '11 19:12

Peter Amsterdam


3 Answers

Something of this kind might be of help:

$(document).ready(function(){
   if ($.cookie('test_status') != '1') {
    //show popup here
    $.cookie('test_status', '1', { expires: 60}); }
   });
like image 91
Sudhir Bastakoti Avatar answered Oct 27 '22 07:10

Sudhir Bastakoti


First include the jquery library.

After include the below script for cookies in jquery.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

Now put the below code into the footer :

$(document).ready(function() {
       // initially popup is hidden:
        $('#stay-in-toch.popup-outer').hide();
        // Check for the "whenToShowDialog" cookie, if not found then show the dialog and save the cookie.
        // The cookie will expire and every 2 days and the dialog will show again.
        if ($.cookie('whenToShowDialog') == null) {
            // Create expiring cookie, 2 days from now:
            $.cookie('whenToShowDialog', 'yes', { expires: 2, path: '/' });

            // Show dialog
             $('#stay-in-toch.popup-outer').show();       
        }
    });
like image 28
Amit Naraniwal Avatar answered Oct 27 '22 07:10

Amit Naraniwal


You can try this

$(document).ready(function() {  
    if ($.cookie('test_status')) {
        return;
    }

    //Rest of your code here
});
like image 37
ShankarSangoli Avatar answered Oct 27 '22 07:10

ShankarSangoli