0

I am trying to make a popup appear on a webpage only once, using Javascript.

So on page load, I create a cookie. Then I have an if statement - if the cookie has already been made, I don't wish to display the popup - so I am fading it out. (Is there a better way to just make this not appear at all?)

I have created a codebox here.

JS here, both areas it is targetting are simple divs :

$(document).ready(function(){
    var visited = $.cookie('visited'); // create the cookie
    if (visited == 'yes') {
       $("#popup").fadeOut();
       $("#popup").fadeOut();
    } 
    else 
    {
    $("#hover").click(function(){
           $(this).fadeOut();
       $("#popup").fadeOut();
      });
    $("#close").click(function(){
           $("#hover").fadeOut();
       $("#popup").fadeOut();
    });
    $.cookie('visited', 'yes', {
        expires: 7 // the number of days the cookie will be effective
    });                    
    }
  });

Unfortunately, none of the Javascripts seems to run - the popup will no longer close or recognize clicks and displays every time the user navigates to the page.

What have I done incorrectly here and how can I fix it ?

1
  • I'd suggest you fadeIn() on missing cookie rather than fadeOut on the existing cookie. your code seems fine except two $("#popup").fadeOut(); lines - one of them probably shouls be #hover, but we need to see the html part for a better answer Commented Sep 11, 2014 at 10:19

1 Answer 1

1

I used the code below to display a webpage only once and ask the visitor to submit their details. If the details are submitted once, from next time the page redirects to another page.

<script>

        go_to = "kwd.php";

        num_days = 180;
        function ged(noDays){
            var today = new Date();
            var expr = new Date(today.getTime() + noDays*24*60*60*1000);
            return  expr.toGMTString();
        }

        function readCookie(cookieName){
            var start = document.cookie.indexOf(cookieName);
            if (start != -1){ 
            window.location = go_to;
            }
        }

        function createcookie() {
            document.cookie = "seenit=yes; expires=" + ged(num_days);
        }

        readCookie("seenit");
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.