Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:28:53+00:00 2026-06-17T19:28:53+00:00

I am trying to find how may I set a javascript cookie so it

  • 0

I am trying to find how may I set a javascript cookie so it displays a fancybox popup after say 4-th pageview of visitor.

Here is the code that I am using to display a Fancybox popup, but as you may see, this displays the code only on first pageview and it expires after a day

function setCookie(c_name,value,exdays)
 {
     var exdate=new Date();
     exdate.setDate(exdate.getDate() + exdays);
     var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
     document.cookie=c_name + "=" + c_value + ";domain=.mysite.net;path=/";
 }

function getCookie(c_name)
 {
     var i,x,y,ARRcookies=document.cookie.split(";");
     for (i=0;i<ARRcookies.length;i++)
  {
     x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
     y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
     x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
       {
       return unescape(y);
       }
  }
 }

jQuery(document).ready(function() {
     var show = getCookie("fancyreg");
     if(show==null || show=="") {
       $.fancybox(
 {
        'type' : 'iframe',
        'href' : 'http://www.mysite.net/popup/', //URL to popup page or image
        'autoDimensions' : false,
        'width' : 480,
        'height' : 260,
        'transitionIn' : 'none',
        'transitionOut' : 'none'
 }
);

  setCookie('fancyreg','1',1); 

 }
 });

I would also like if you may help me how to add a delay in my existing code so teh popup display after 3 seconds (3000 ms).

I tried with setTimeout(function() as below

    <script type="text/javascript"> 
jQuery(document).ready(function() {
    setTimeout(function() {
        $.fancybox({
            'type' : 'iframe',
        'href' : 'http://www.mysite.net/popup/', //URL to popup page or image
        'autoDimensions' : false,
        'width' : 480,
        'height' : 260,
        'transitionIn' : 'none',
        'transitionOut' : 'none'
        })
    }, 4000); 
});
</script>

but it does not work.

As for controlling the pageviews, I have no idea how to set nor I could find any resource to help me through this.

Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:28:54+00:00Added an answer on June 17, 2026 at 7:28 pm

    Update April 02, 2019

    As mentioned in the comments by @Gregdebrick the answer is being update to use the JavaScript Cookie plugin https://github.com/js-cookie/js-cookie instead of the deprecated jQuery Cookie plugin.

    So, after loading the JS Cookie plugin from CDN in your page:

    <script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
    

    Use the following script:

    $(document).ready(function () {
        // if the cookie is undefined, create it
        if(typeof Cookies.get('visited') === "undefined"){
            Cookies.set("visited", 0);
        }
        // Cookies.get('visited') returns a string
        if (parseInt(Cookies.get('visited')) >= 3) {
            // open fancybox after 3 secs on 4th visit
            setTimeout(function () {
                $.fancybox.open({
                    // your fancybox API options here
                });
            }, 3000);
        } else {
            let increase = parseInt(Cookies.get('visited')) + 1;
            Cookies.set('visited', increase, { expires: 1 });
            return false;
        }
    }); // ready
    

    See updated working DEMO


    Assuming that you are using the jQuery Cookie Plugin, then you could use the following code to launch fancybox after 3 seconds, the 4th page visit on the same day :

    $(document).ready(function () {
        // create cookie
        var visited = $.cookie('visited'); // visited = 0
        if (visited >= 3) {
            // open fancybox after 3 secs on 4th visit or further on the same day
            setTimeout(function () {
                $.fancybox.open({
                    // your fancybox API options here
                });
            }, 3000);
        } else {
            visited++; // increase counter of visits
            // set new cookie value to match visits
            $.cookie('visited', visited, {
                expires: 1 // expires after one day
            });
            return false;
        }
    }); // ready
    

    See working DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to find records in a VARCHAR column that may contain a NUL
I am trying to find out whether local press releases may have affected companies'
I am trying to set the contents of .myShop, after it is loaded from
I've been trying to find the answer to this question here. Several people seem
I was trying to find some examples on how to find a given set's
I'm trying to find a tidy way to set a string using an if
I'm using JavaScript to set the value of an input with text that may
I'm trying to set up a javascript front in that is consuming JSON from
Where can I find documentation on running JavaScript code inside a PDF? I've never
Trying to find a standard. The CmdLet will process data - multiple input, defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.