0

i am using fancybox popup in jQuery,

how can I make this appear every time a user enters at my site? Preferably showing it one time per session…

here is the example how do i implement / set cookie

http://www.sohtanaka.com/web-design/examples/modal-window/index2.htm

2 Answers 2

0

Create a cookie once the fancybox opens using:

'onComplete' : function() {
        // set cookie
    }

but always check for the existence of the cookie when before opening the fancybox. Do not set an 'expire' value and this will force the cookie to be deleted when the session ends.

Edit:

Just extending the code you have posted:

$(document).ready(function(){

    var check_cookie = $.cookie('the_cookie');

    if(check_cookie == null){

        $.fancybox('<h2>Some content</h2>', {
            'onComplete' : function() {
                    $.cookie('the_cookie', 'the_value');
            }
        });
    }
});

If the page loads and the script can't find the cookie, the fancybox opens. If the page loads and cookie exists, the fancy box doesn't open.

There is some further info about opening fancybox when the page loads here.

2
  • thanks digvyswift but i am not smart in js can you please write in detail ? or should i put this after Aug 7, 2011 at 18:28
  • thanks frnd ! i am nt so smart in js i got this code from somewhere & put after // $(document).ready(function(){ var check_cookie = $.cookie('the_cookie'); if(check_cookie == null){ $.cookie('the_cookie', 'the_value'); //fire your fancybox here } }); but not working news.shreshthbharat.in/fancy-box_onload/index-2.html Aug 7, 2011 at 18:33
0

You need to use the cookie plugin for this to work - https://github.com/carhartl/jquery-cookie

Then use the code Digbyswift posted... Works great!

$(document).ready(function(){

    var check_cookie = $.cookie('the_cookie');

    if(check_cookie == null){

        $.fancybox('<h2>Some content</h2>', {
            'onComplete' : function() {
                    $.cookie('the_cookie', 'the_value');
            }
        });
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.