Simple Automatic Popup Window using JavaScript

Automatic Popup Window in JavaScript

Do you want to create an Automatic Popup Window using JavaScript?

OK, in this article you will learn how to create JavaScript Automatic Popup Window. Popup box is a popular element for any webpage. In many places, this type of modal box is used to show any information to the user.

Suppose you ask a user to subscribe to your website. In that case, you can use this type of Automatic Popup box. In many other cases such as login forms, newsletters, ads, etc. What is needed to create this Automatic Popup Window

   ✅  Basic structure by HTML

   ✅  Need to design by CSS

   ✅  JavaScript to activate the popup

Do you think this type of Automatic Popup Window JavaScript is very difficult to create
In fact, you can build it using simple code.

Create Automatic Popup Window in JavaScript

The design made in this article is made by HTML, CSS, and javascript. I have created many popup boxes before without using JavaScript. However, using JavaScript will make this task much easier. You can customize it as you popup.

See the Pen Untitled by Ground Tutorial (@groundtutorial) on CodePen.


If you want to create an Automatic Popup Window and want a step-by-step tutorial then keep reading till the end of the article. After each step here, I have shown the possible results with screenshots.

1. Make a box on the webpage

I first created a basic box using the following HTML codes. All the information can be found in this box. And it will act as a pop-up box.


<div class="popup">

</div>

Some amount of CSS has been used here to design the webpage. White color has been used in the background of the box. 

Depending on the width of the box: 420px and height padding: 30px 40px. Box-shadow has been used to enhance beauty.


Here I have used display: none. As a result, this box cannot be viewed under normal circumstances. The image below is just to show what changes can be made using these codes.

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body{
    background-color: #8fbaec;
}

.popup{
    background-color: #ffffff;
    width: 420px;
    padding: 30px 40px;
    position: absolute;
    transform: translate(-50%,-50%);
    left: 50%;
    top: 50%;
    border-radius: 8px;
    font-family: "Poppins",sans-serif;
    display: none;
    text-align: center;
    box-shadow: 5px 5px 30px rgba(0,0,0,.2);
}

Make a box on the webpage

2. Add a profile image in the Popup Window

Added an icon. You can use a profile image instead of this icon. Although I have used this content for design. You can remove these contents when you use them in your professional work.


<div class="fab fa-youtube icon1"></div>


.icon1{
  font-size: 60px;
  background: red;
  height: 120px;
  width: 120px;
  color: white;
  border-radius: 50%;
  line-height: 120px;
  text-align: center;
}


Add a profile image in the Popup Window

3. Create a button to cancel the Popup Window

Now you need to create a cancel button. This button allows the user to hide the Automatic Popup Window HTML. You must keep this button even if you use it for your professional work. There is a cross icon on the button.


<button id="close">&times;</button>


.popup button{
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  margin: 10px 10px auto;
  background-color: transparent;
  font-size: 30px;
  color: #ffffff;
  background: #03549a;
  border-radius: 100%;
  width: 40px;
  height: 40px;
  border: none;
  outline: none;
  cursor: pointer;
}

Create a button to cancel the Popup Window

3. Headings and some basic text

Now I have added some more information here. In which I first added a heading and then added some basic paragraphs.


<h2> Ground Tutorial </h2>
<p>Lorem, ipsum dolor sit amet .... iure qui</p>



h2{
  margin-top: 10px;
  font-size: 25px;
}

.popup p{
  font-size: 14px;
  text-align: justify;
  margin: 20px 0;
  line-height: 25px;
}

Headings and some basic text

4. Created a simple button

Now I have created a simple button here. This button is for design only. You can remove these contents while using them in your profession. The button depends on width: 150px and height padding: 10px 0.


<a href="#">Subscribe Here</a>


a{
    display: block;
    width: 150px;
    position: relative;
    margin: 10px auto;
    font-size: 17px;
    text-align: center;
    background-color: red;
    border-radius: 10px;
    color: #ffffff;
    text-decoration: none;
    padding: 10px 0;
}

Automatic Popup Window using JavaScript

5. JavaScript of the Automatic Popup Window

We have designed the above. Now is the time to make it work. Some basic amounts of JavaScript have been used to make it work. Whether you know Basic JavaScript or not, you can understand.

   ✅ The 'load' function is used here. This means that the following codes will work when your webpage is loaded.
   ✅ Conditions have been added to 'setTimeout' here. And here the time is set to 2000 milliseconds.

   ✅ Here '.popup' has 'display = block'. As a result, we see the popup box.


As a result of using 2000, you will see the Popup Window after 2 seconds. If you want to see the Automatic Popup Window after 3 seconds, then use 3000 milliseconds.


window.addEventListener("load", function(){
  setTimeout(
    function open(event){
       document.querySelector(".popup").style.display = "block";
    },
     2000
  )
});


Now the close button has been activated. Simple 'display = none' is used here. When you click on the button, it will be 'display = none' and the Automatic Popup Window will be hidden.


document.querySelector("#close").addEventListener("click", function(){
  document.querySelector(".popup").style.display = "none";
});

You may have problems copying the above codes and assembling them together. So below I have provided a download button. Using this button you can download the code to create Automatic Popup Window javascript.

First, follow the tutor above and find out how it was created. Then download the codes directly using the button below. We hope you find out from this tutorial how Automatic Popup Window is created using JavaScript.







Comments

Popular posts from this blog

Simple Day Night Toggle Button Using HTML and CSS

Splash Image Mask Using HTML and CSS (Free Code)

How To Limit Character In Input Field using JavaScript