在Tumblr的年龄门飞溅

时间:2016-04-02 17:21:36

标签: cookies tumblr

我正在尝试将此脚本http://codepen.io/blixxurd/pen/PZQVMy添加到tumblr页面,并且没有运气使按钮正常工作。

看起来很棒,显示在页面加载但单击按钮似乎什么都不做。任何建议都会很棒。

以下是javascript:

    document.addEventListener("DOMContentLoaded", function(event) {

  var cookieHandlers = {

    createCookie : function(name,value,days) {
      if (days) {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    },

    readCookie : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },

    eraseCookie : function(name) {
        cookieHandlers.createCookie(name,"",-1);
    }
  };

  var ageGate = {
    element  : document.getElementById("ageGate"),
    sorryWarning  : document.getElementById("ageSorryWarning"),
    contentWrap : document.getElementById("ageContentWrap"),
    cookieName  : "forever21_or_older",
    select   : function(e) {
      if(e=="yes") {
        cookieHandlers.createCookie(ageGate.cookieName, "true", 30);
        ageGate.actions.hide();
      } else {
        ageGate.contentWrap.style.display = "none";
        ageGate.sorryWarning.style.display = "block";
      }
    },
    actions : {
      show  : function() {
        ageGate.element.style.display = "block";
      }, 
      hide  : function() {
        ageGate.element.style.display = "none";
      }
    }
  };

  //Pop Age Gate on Page Load
  if(cookieHandlers.readCookie(ageGate.cookieName)==null) {
    ageGate.actions.show();
  }

});

这是HTML:

<div id="ageGate">
  <div  class="age-content-wrapper" id="ageSorryWarning">
    <strong>Sorry, you must be 21 years of age or older to view this site.</strong><br><br>
    <a class="btn btn-primary" href="http://google.com/">Leave Website</a>
  </div>
  <div class="age-content-wrapper" id="ageContentWrap">
    <strong class="age-heading">Are you at least 21 years old?</strong>
    <div class="age-controls">
      <a href="javascript:;" onclick="ageGate.select('yes')" class="btn btn-large btn-success">Yes</a>
      <a href="javascript:;" onclick="ageGate.select('no')" class="btn btn-large btn-danger">No</a>
    </div>
    <span class="age-subhead">This website facilitates the sale of alcoholic beverages. We use cookies to assist in verifying age.</span>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

以下是修复:

  var cookieHandlers = {

    createCookie : function(name,value,days) {
      if (days) {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    },

    readCookie : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },

    eraseCookie : function(name) {
        cookieHandlers.createCookie(name,"",-1);
    }
  };

  var ageGate = {
    element  : null,
    sorryWarning  : null,
    contentWrap : null,
    cookieName  : "forever21_or_older",
    select   : function(e) {
      if(e=="yes") {
        cookieHandlers.createCookie(ageGate.cookieName, "true", 30);
        ageGate.actions.hide();
      } else {
        ageGate.contentWrap.style.display = "none";
        ageGate.sorryWarning.style.display = "block";
      }
    },
    actions : {
      show  : function() {
        ageGate.element.style.display = "block";
      }, 
      hide  : function() {
        ageGate.element.style.display = "none";
      }
    }
  };


document.addEventListener("DOMContentLoaded", function(event) {
  ageGate.element = document.getElementById("ageGate");
  ageGate.sorryWarning  = document.getElementById("ageSorryWarning");
  ageGate.contentWrap = document.getElementById("ageContentWrap");

    //Pop Age Gate on Page Load
  if(cookieHandlers.readCookie(ageGate.cookieName)==null) {
    ageGate.actions.show();
  }

});