JavaScript Age confirmation Overlay

I designed an age verification popup with the help of some online tutorials and a friend due to my limited knowledge of JavaScript.

Check it out live here-

My issue is that I want this popup to load/appear after the page content loads, but I'm not sure how to implement it.

JavaScript Code-

<script type="text/javascript">
 function confirmAge(){
var d = new Date();
//var time_stmp = Math.round(d.getTime()/1000);
var now_us = d.getTime();


var myDate=document.getElementById('day').value +"-"+ document.getElementById('mon').value +"-"+ document.getElementById('yer').value;
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
var date_us=new Date(newDate).getTime();

var age_us=(now_us-date_us)/(1000*356*24*3600);


if(age_us<18){
    alert("You must be 18 years of age to see this site.");
    return false;
}
else
    document.getElementById('ac-wrapper').style.display="none"; 

   }
</script>

CSS Stylesheet-

<style>
#ac-wrapper {
position            : absolute;
top                 : 0;
left                : 0;
width               : 100%;
height              : 820px;
background: rgba(255,255,255,.6);
z-index             : 1001;
}
#popup{
 width: 555px;
height: 375px;
background: #FFFFFF;
border: 5px solid #000;
border-radius: 25px;
-moz-border-radius: 25px;
-webkit-border-radius: 25px;
 box-shadow: #64686e 0px 0px 3px 3px;
-moz-box-shadow: #64686e 0px 0px 3px 3px;
-webkit-box-shadow: #64686e 0px 0px 3px 3px;
position: relative;
top: 150px; left: 375px;
 }
 #popup_logo{
position: relative;
left: 130px; top: 30px;
 }
 #popup_form{
text-align: center;
 }
 #popup_submit{
width: 120px;
height: 47px;
background: url(images/Enter-button.png) no-repeat;
cursor: pointer;
border: 0px;
 }
 p{
font: bold 20px Tahoma;
color: #000;
text-align: center;
 }
</style>

HTML Markup-

    <div id="ac-wrapper">
      <div id="popup"> <img src="images/store_logo.png" alt="D elicious Liquid Vapor" id="popup_logo"><br />
        <br />
        <br />
        <br />
        <p>To play with the bull, you must be 21 years of age.<br />
          Please enter your birth date...</p>
        <br />
        <div id="popup_form">
          <select name="birthmonth" id='mon'>
            <option value="1">January</option>
            ......................
            <option value="12">December</option>
          </select>
          <select name="birthday" id='day'>
            <option value="1">1</option>
            <option value="2">2</option>
            .....................
            <option value="31">31</option>
          </select>
          <select name="birthyear" id="yer">
            <option value="2007">2013</option>
            ..............
            <option value="1900">1900</option>
          </select>
          <br />
          <br />
          <input type="button" id="popup_submit" name="submit" value="" onClick="confirmAge()" />
        </div>
      </div>
    </div>

Answer №1

In order to display a popup after the content has loaded, you will need to implement an asynchronous callback function. This can be done either when the DOM is ready or when all content has finished loading, which could take too long.

Check out the following link for more information:

http://javascript.info/tutorial/onload-ondomcontentloaded

Your code will require some restructuring, similar to this example:

function renderMyPopup() {
 // implementation goes here 
}

Then, include the asynchronous callback like this:

onload = function() {
    renderMyPopup()
}

Alternatively, using jQuery can simplify this process. Check out this link for more information:

$(document).ready shorthand

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my css stylesheet to not function properly in Sinatra?

I'm fairly new to this, but based on my research, this code should be functioning correctly. I am attempting to link a CSS stylesheet to an .erb file. Here is the code snippet I am using: <link rel="stylesheet" type="text/css" h ...

What strategies can be implemented to minimize the impact of HTML code on just one specific div (or any other chosen element)?

<div> <b> some example </div> different content This will make the "different content" bold. Is there a way to restrict the impact of <b> only to the <div> element? I have tried looking for JavaScript solutions online but did ...

Is it possible to adjust the viewport to a size larger than 1024?

This site we're examining is not responsive and has a width of about 1230 pixels. When viewed on mobile devices, some content is cut off from the right side due to compression, resulting in approximately a 10% loss. The issue becomes more noticeable ...

transfer jquery element using jquery ajax

How can I send a jQuery object to a PHP function using POST? When I stringify the object, I get the following output: [ { "id": "701", "user_id": "2", "playlist": "ukg%20garage", "tracks": "5", "thumbnail": "Coldplay.jpeg", "crea ...

Closing a live search box by tapping away from it

The link I found as the best example for this topic is: http://www.example.com In the example provided, if you enter a keyword in the search field, suggestions will drop down. I have implemented this code but would like to make a slight modification. I w ...

Guide on transferring Javascript array to PHP script via AJAX?

I need to send a JavaScript array to a PHP file using an AJAX call. Here is the JavaScript array I am working with: var myArray = new Array("Saab","Volvo","BMW"); This JavaScript code will pass the array to the PHP file through an AJAX request and displ ...

React fails to display the content

Starting my React journey with Webpack configuration. Followed all steps meticulously. No error messages in console or browser, but the desired h1 element is not showing up. Aware that React version is 18, yet working with version 17. App.jsx import Reac ...

Switching from a vertical to horizontal tab layout in Angular 4 Material 2 using MD-Gridlist

I'm currently trying to modify the tabbing functionality within an MD-Gridlist so that it tabs horizontally instead of vertically. I've experimented with tab indexes but haven't had any success. My goal is to enable horizontal tabbing throug ...

Effortless form submission with jQuery plugin and CakePHP 2 using AJAX

Trying to figure out how to use the jquery form plugin to upload an image via ajax. I'm looking to display the saved version of the image on the same page that initiated the request. Although I'm new to cakephp and jquery, I've been grapplin ...

AngularJS: when ng-route clashes with Bootstrap datetimepicker

I have nearly completed my calendar app and I am looking to integrate the datetimepicker for updating events. You can find the datetimepicker that I am using at this link: Below is an example of how I have utilized it in HTML: <div class="dropdown" s ...

Transfer real-time information to MVC controller through AJAX

Is there a way to send live data using an AJAX request to an MVC Controller? Controller: public JsonResult ApplyFilters(dynamic filters){ return null; } The AJAX call: $(':checkbox').click(function (event) { var serviceIds = $('in ...

Is there a jQuery equivalent to Actionscript's updateAfterEvent function?

Does anyone know of an updateAfterEvent function similar to jQuery in AS3.0? I've been experimenting with mouse events (mousemove, mousedown, mouseup) while resizing container divs. Although it's smooth, I'm curious if there's a way to ...

Creating a custom HTML table layout with both variable and fixed column widths, along with grouping headers

Is there a way to format an HTML table so that it appears like this: <- full page width -> <-20px->< dynamic ><-20px->< dynamic > +------------------+-------------------+ ¦ A ¦ B ...

"Mastering the art of displaying real-time data on a thermometer using D3.js

Welcome to the sample code for a thermometer created using D3.js. You can view the code on jsfiddle. I've developed a web page displaying a dynamic thermometer with values updating every second. Here's the function: setInterval(function(){ getN ...

Does jQuery adhere to the 508 compliance guidelines?

I am currently developing a website using asp.net that must adhere to section 508 accessibility standards. I am unsure about whether I should incorporate JavaScript into the site. Is it permissible to utilize jQuery's slideUp()/slideDown() for displa ...

The CanJS model is unable to retrieve data from a .json file

Implementing MVC using AMD in canjs with requirejs has been my current focus. Here's a look at my domains.json file: [ "1":{"uid": "1","urls": "domain1.abc.com"}, "2":{"uid": "2","urls": "domain2.abc.com"}, "3":{"uid": "3","urls ...

Transform data into JSON format using AJAX in CodeIgniter

I am having trouble with converting json code in CodeIgniter for pagination. I am using ajax to control the data conversion to json and sending it to the view. Can someone please explain how to do this? In the controller, instead of using a foreach loop, ...

Set the datepicker to automatically show today's date as the default selection

The date picker field is functioning correctly. I just need to adjust it to automatically display today's date by default instead of 1/1/0001. @Html.TextBoxFor(model => model.SelectedDate, new { @class = "jquery_datepicker", @Value = Model.Selecte ...

Encountering issues while trying to install Java using NPM

Encountering errors when attempting to run the following command to install java dependencies for NPM. NPM install -g java In need of assistance to fix this error. C:\WINDOWS\system32>npm i -g java [email protected] install C:\D ...

The sluggish rendering speed of AngularJS is causing a delay in displaying the loader

Within my application, I have tabs that load a form in each tab with varying numbers of controls. Some tabs may contain a large number of controls while others may have fewer. My goal is to display a loader while the contents are being rendered into the HT ...