views:

37

answers:

2

I'm using the following code to display two div containers after 3 minutes:

/* Display After 3 Minutes (180000) */
setTimeout("jQuery('#contact-overlay, #contact-window').show();", 180000);

I only want these div containers to open after 3 minutes on the first 4 visits to the website, after this the div containers should not open for that user again. I assume this could be tracked using the jquery cookies script, but I'm not quite sure how I would get this working.

+1  A: 

There are jQuery plugins to manage cookies. Take a look to this:

http://www.electrictoolbox.com/jquery-cookies/

greuze
A: 

Get the cookie plugin from the link here: http://www.electrictoolbox.com/jquery-cookies/

Open only on the first 4 visits to the site:

   //$(document).ready(function(){
       if(!$.cookie('visits')){
         $.cookie('visits',1, {expires: 30});
       }
       $.cookie('visits', (parseInt($.cookie('visits')) + 1), {expires: 30}) 
           //will expire after 30 days
       var overlayContact = function(){
           if(parseInt($.cookie('visits')) < 5){
               $('#contact-overlay, #contact-window').show();
           }
       };
       setTimeout(overlayContact, 180000);
   //} 
   // Replace all $ with jQuery if you're doubling up 
   // on your javascript frameworks.

Or to open only on the first 4 times the user actually sees the box, move the cookie incrementing line into the body of the overlayContact function, before the if.

Tim Snowhite
@Tim Snowhite: Using either of the above codes I can only get the div to load the first time and that's it. If I clear the cookies it loads the first time again. However it does not load the first 4 times. Any idea why?
GhostPool
@GhostPool: try with the updated code. You can also set breakpoints and see how the code is running using Firebug in Firefox or the inspector in Google Chrome or Safari. Those should help you debug the code.
Tim Snowhite
@Tim Snowhite: Works a charm. Thanks. :)
GhostPool