callmedpit callmedpit - 4 months ago 24
Javascript Question

Reveal Modal with cookie to display only once

I'm using the (Reveal Modal) and loading it on page by using this code:

$(document).ready(function() {
$('#myModal').reveal();
});


How can I modify this, I believe using cookies (is there another way?), so that it will only pop one time for a user over the course of lets say 1 week?

Answer

Use the jquery-cookie plugin by carhartl.

Check for cookie before showing the modal. If it's present, don't display it. If it's not, store a new cookie and display modal.

$(document).ready(function() {
    if ($.cookie('modal_shown') == null) {
        $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
        $('#myModal').reveal();
    }
});