Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Newbie: Trouble with popup form and cookies

    I'm trying to create a popup sign up form that will only popup every week. I don't want it to popup every time someone visits the site.

    I'm new to Javascript as a whole and can't seem to figure out how to set a cookie for a week and if there is no cookie (or the cookie has expired) it will popup automatically.

    If anyone can point out what I'm doing wrong it would be greatly appreciated. I feel like I'm so close and that the solution is staring me in the face.

    Here's the code that I have so far:

    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);
    }
    }
    }

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

    function checkCookie()
    {
    var username=getCookie(exdate);
    if (exdate!=null && exdate!="")
    {
    //Do Nothing
    }
    else
    {

    $(document).ready(function() {

    var id = '#dialog';

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    $('#mask').fadeIn(1000);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top', winH/100-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //transition effect
    $(id).fadeIn(1000);
    $(id).animate({top:"+=300px"},1000);

    //if close button is clicked
    $('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $('.window').animate({right:"+=150px"}).animate({left:"1500px"}).fadeOut(50);
    $('#mask').hide();
    });

    //if NO Thanks button is clicked
    $('.window .noThanks').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $('.window').animate({right:"+=150px"}).animate({left:"1500px"}).fadeOut(50);
    $('#mask').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
    $(this).hide();
    $('.window').hide();
    });

    function hideHolder() {
    $('.place-holder').hide();
    }
    function showHolder() {
    if($('.name-field').val() == '') {
    $('.place-holder').show()
    }
    }
    $('.name-field').focus(hideHolder);
    $('.name-field').blur(showHolder);
    $('.place-holder').click(function () {
    $('.name-field').trigger('focus');
    });
    });
    }
    }


    Thank you for your help. :-)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    All modern browsers will block unrequested (automatic) popups. There are plenty of cookie scripts to be found if you use the search feature of this forum. This question is asked once a week at least.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    codeboy (02-29-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •