0

使用 Nation Builder 作为客户网站的平台,需要创建年龄门。我会在单独的页面上进行年龄验证,但显然它不适用于这个平台,所以我使用 Jquery 对话框显示在主页顶部。我验证年龄的代码有效,但需要对其进行调整以关闭小部件 UI 而不是 url 重定向。

我不是编程专家,因此将不胜感激。这是一个链接。http://patricialourenco.com/test.html 干杯!

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";
    }
  }
}
4

1 回答 1

0

我认为您正在寻找 .close 方法 - 这是您在用户按下按钮提交年龄后使用它的方式:

$('#dialog-modal').dialog('close'); //hides modal, shows content behind

更多关于 jQuery UI 对话框的文档:

http://api.jqueryui.com/dialog/#method-close


编辑:这是您可以更新 start() 方法的方法:

 start: function() {
   this.setUsersBirthday();
   if (this.userIsOverMinimumAge()) {
     this.setCookie("usersBirthday",this.usersBirthday,30);
     $('#dialog-modal').dialog('close');
   } else{
     this.notMinimumAge();
   };
 }, 

还有一个 JSFiddle 演示:http: //jsfiddle.net/donmccurdy/czERa/


编辑#2 - (回应OP关于检查每个页面上的cookie的问题)

检查 cookie 的代码中存在一些错误。我已经更新了 JSFiddle,这是错误的快速版本:

1) 先检查 cookie,而不是您当前始终显示对话框的代码。像这样:

$(function() {
    window.ageCheck.checkCookie();
});

2) 更新 checkCookie() 方法以在必要时显示对话框:

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)您的 getCookie 方法中有一个错误,因为您返回的是“usersBirthday”而不是生日本身。固定在这里:

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]);
    }
  }
},
于 2013-06-06T15:53:07.760 回答