...

View Full Version : Newbie: Trouble with popup form and cookies



codeboy
02-28-2012, 11:21 PM
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. :-)

Philip M
02-29-2012, 09:06 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum