Question

I want to display a pop-up on home page, but only once. If someone goes to other pages and then comes back to home page, I don't want to display it again.

Here's the code:

<script type="text/javascript" language="javascript">
    $(document).ready(function(){
    $("#displaybox").hide();
    message: $('#displaybox'),
        css: {
            top:  ($(window).height() - 391) /2 + 'px',
            left: ($(window).width() - 556) /2 + 'px',
            width: '556px'
           }
    });
    $('.displayboxclose').attr('title','Click to close').click($.unblockUI);

    setTimeout($.unblockUI, 30500);

});
</script>

Can someone help with inserting the cookie code?

I'm using jQuery BlockUI Plugin and jquery-cookie Plugin

Was it helpful?

Solution

Something like this might be usefull for you;

jQuery(function($) {
    if (!$.cookie('blockuicookie')) {
        // blockUI scripts goes here.
        $.cookie('blockuicookie', true, { "expires": 30, "path": "/" });
        setTimeout($.unblockUI, 30500);
    }
});

OTHER TIPS

You dont need to use a jquery cookie plugin:

function setCookie(name, value, expires, path, domain, secure) {
    var caution = false;
    var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    if (!caution || (name + "=" + escape(value)).length <= 4000)
        document.cookie = curCookie;
        else
            if (confirm("Cookie exceeds 4KB and will be cut!"))
                document.cookie = curCookie;
}

//name - name of the cookie
//* return string containing value
//of specified cookie or null if cookie
//does not exist
function getCookie(name) {
    var prefix = name + "=";
    var cookieStartIndex = document.cookie.indexOf(prefix);
    if (cookieStartIndex == -1)
        return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
                prefix.length);
                if (cookieEndIndex == -1)
                    cookieEndIndex = document.cookie.length;
                    return unescape(document.cookie.substring(cookieStartIndex +
                            prefix.length,
                            cookieEndIndex));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

To set a cookie call this way:

setCookie("dateModified","11/14/2013");

To get a cookie:

var lastDate = getCookie("dateModified");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top