Search code examples
javascriptjquerycookiesfancyboxjquery-cookie

Fancybox Modal with jquery cookie to open once on page load


I have a modal that opens in fancybox. I can fire this modal on page load by clicking the open modal button with jquery:

$("#download-brochure-button").fancybox().trigger('click');

I am trying to set a cookie so that this only opens once per session. I am looking in the chrome inspector under application tab and I cannot see any cookie being generated. I am using jQuery Cookie to handle the cookie but I must be doing something wrong. Here is my js:

//Brochure Modal with cookie
$(document).ready(function(){
    var check_cookie = $.cookie('brochure_modal_cookie');
    if(check_cookie === null){
        $.cookie('brochure_modal_cookie', 'value', { expires: 7 });
        $("#download-brochure-button").fancybox().trigger('click');
   }
});

Currently no modal shows and no cookie can be seen. Any and all help greatly appreciated.


Solution

  • I fixed this by using the updated https://github.com/js-cookie/js-cookie plugin.

    My working code:

    //Brochure Modal with cookie
    Cookie = Cookies.get('brochure_modal_cookie');
    if (Cookie == null) {
        $("#download-brochure-button").fancybox().trigger('click');
        Cookies.set('brochure_modal_cookie', 'value', { expires: 7 });
    }