2

I saw the age-verification snippet solution for my problem and it worked great. But in that solution the occurrence of that popup is based on the number of days. I want the popup to occur every time the user enters my website. How to do it?

Here's is the JS part of the snippet code.

<script>
 function ageCheck() {
   var min_age = {{ age }};  // Set the minimum age. 
   var year =   parseInt(document.getElementById('byear').value);
   var month =  parseInt(document.getElementById('bmonth').value);
   var day =    parseInt(document.getElementById('bday').value);
   var theirDate = new Date((year + min_age), month, day);
   var today = new Date;
   if ((today.getTime() - theirDate.getTime()) < 0) {
     window.location = 'http://google.com'; //enter domain url where you would like the underaged visitor to be sent to.
   } else {
     var days = 1; //number of days until they must go through the age checker again.
     var date = new Date();
     date.setTime(date.getTime()+(days*24*60*60*1000));
     var expires = "; expires="+date.toGMTString();
     document.cookie = 'isAnAdult=true;'+expires+"; path=/";
     location.reload();
   };
  };
  function readCookie(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;
  };
  var isAnAdult = readCookie('isAnAdult');
  if (isAnAdult) {
    document.write("<style> #prompt-background { display: none; }</style>");
  };
</script>

and the button that function is called on

<button id="submit_birthdate" class="exitbutton" onclick="ageCheck()">Enter</button> 

How to modify this code so that the popup appears every time?

Megha Sirisilla
  • 151
  • 2
  • 12
  • You literally have a variable with a comment that reads `number of days until...`. Change the value of the variable or remove the cookie. – Cray Apr 24 '19 at 05:47
  • @Cray when I remove the cookie...on "Enter" button instead of entering my website, it's the popup that is populated again. It's like a cycle. I don't know Javascript much so couldn't understand how change the occurrence from number of days. – Megha Sirisilla Apr 24 '19 at 05:52
  • 2
    call onload method to load a script on page load. Possible duplicate of https://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load – Omi Harjani Apr 24 '19 at 05:58
  • As I understand the cookie's value is used to hide prompt? I don't see a reason here why it would not work if you either set `days = 0` or `isAnAdult=false` or remove all the parts with cookie since you don't seem to need it at all. – Cray Apr 24 '19 at 06:05
  • @Cray If I set days = 0 then the same thing is happening. The popup is being populated again instead of being redirected to the website. I think I need the cookie to stay active till the user is active on the website and expire as soon as the user exits. – Megha Sirisilla Apr 24 '19 at 06:17
  • 2
    Use [session cookie](https://stackoverflow.com/questions/14196671/session-only-cookies-with-javascript) instead of one with expiry date. – Cray Apr 24 '19 at 06:24
  • @Cray thank you so much. setting expires = 0 did it for me. – Megha Sirisilla Apr 24 '19 at 07:02

0 Answers0