Using Nation Builder as a platform for a client's site and need to create an age gate. I would have the age verification on a separate page but apparently it won't work with this platform, so I'm using a Jquery dialog to show on top of the home page. The code I got to verify age works, but need to tweak it in order to close the widget UI instead of a url redirect.
I'm no programming expert so any help and dumming it down would be appreciated. Here's a link. http://patricialourenco.com/test.html Cheers!
var ageCheck = {
//Set the minimum age and where to redirect to
minimumAge : 13,
userIsOldEnoughPage : "http://www.bullyproject.com",
userNotOldEnoughPage : " http://www.pacerkidsagainstbullying.org/#/home",
//Leave this stuff alone please :)
start: function() {
this.setUsersBirthday();
if (this.userIsOverMinimumAge()) {
this.setCookie("usersBirthday",this.usersBirthday,30);
window.location = this.userIsOldEnoughPage;
} else{
this.notMinimumAge();
};
},
usersBirthday : new Date(),
setTheMonth : function() {
var selectedMonth = document.getElementById("month").value;
if (selectedMonth === "1") {
this.setDaysForMonth(29);
}
else if (selectedMonth === "3" ||
selectedMonth === "5" ||
selectedMonth === "8" ||
selectedMonth === "10") {
this.setDaysForMonth(30);
}
else {
this.setDaysForMonth(31);
};
},
setDaysForMonth : function(x) {
var daySelectTag = document.getElementsByName('day')[0];
daySelectTag.options.length = 0;
for(var i=1; i <= x; i++) {
daySelectTag.options.add(new Option(i, i));
}
},
setUsersBirthday: function() {
var usersMonth = document.getElementById("month").value;
var usersDay = document.getElementById("day").value;
var usersYear = document.getElementById("year").value;
this.usersBirthday.setMonth(usersMonth);
this.usersBirthday.setDate(usersDay);
this.usersBirthday.setFullYear(usersYear);
},
setMinimumAge: function() {
var today = new Date();
today.setFullYear(today.getFullYear() - this.minimumAge);
return today;
},
notMinimumAge : function() {
window.location = this.userNotOldEnoughPage
},
userIsOverMinimumAge: function () {
if (this.usersBirthday < this.setMinimumAge()) {
return true;
}
},
setCookie: function (c_name,value,exMinutes) {
var exdate=new Date();
exdate.setMinutes(exdate.getMinutes() + exMinutes);
var c_value=escape(value) + ((exMinutes==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
},
getCookie: function (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("="));
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(x);
}
}
},
checkCookie: function () {
var usersBirthday=this.getCookie("usersBirthday");
if (usersBirthday==null || usersBirthday=="") {
window.location = "ageCheckTester.html";
}
}
}
I think you're looking for the .close method - here's how you'd use it, after the user has pushed the button to submit their age:
$('#dialog-modal').dialog('close'); //hides modal, shows content behind
More documentation on jQuery UI dialogs:
http://api.jqueryui.com/dialog/#method-close
EDIT: Here's how you could update your start() method:
start: function() {
this.setUsersBirthday();
if (this.userIsOverMinimumAge()) {
this.setCookie("usersBirthday",this.usersBirthday,30);
$('#dialog-modal').dialog('close');
} else{
this.notMinimumAge();
};
},
And a JSFiddle Demo: http://jsfiddle.net/donmccurdy/czERa/
EDIT #2 - (Responding to OP's question about checking the cookie on every page)
You had a few bugs in the code that checks the cookie. I've updated the JSFiddle, and here's a quick version of what was wrong:
1) Instead of the code you currently have that always shows the dialog, check the cookie first. Like this:
$(function() {
window.ageCheck.checkCookie();
});
2) Update checkCookie() method to show the dialog when necessary:
checkCookie: function () {
var usersBirthday=this.getCookie("usersBirthday");
if (!usersBirthday) {
// unknown age - show dialog.
$( "#dialog-modal" ).dialog({modal: true});
return;
}
this.usersBirthday = new Date(usersBirthday);
if (this.userIsOverMinimumAge()) {
$('#dialog-modal').hide();
// user is > 13, don't show dialog.
} else {
// user is < 13, redirect to another page.
this.notMinimumAge();
}
}
3) You have a bug in your getCookie method, as you were returning "usersBirthday" instead of the birthday itself. Fixed here:
getCookie: function (c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
//split on '=', then check cookie
x=ARRcookies[i].split('=');
if (x[0]===c_name){
return unescape(x[1]);
}
}
},