Javascript popup that shows only 2x

Karan Goyal
2 min readJan 16, 2023

--

Javascript popup modal that shows up automatically after 2 seconds of page load and shows only 2 times per day per user. I’m using cookies to keep track of the number of times the popup closed.

<style>
/* The Modal (background) */
.ebcf_modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 100000; /* Sit on top */
/* padding-top: 100px; Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
justify-content: center;
align-items: center;
}

/* Modal Content */
.ebcf_modal-content {
/* background-color: #fefefe; */
margin: auto;
/* padding: 20px; */
/* border: 1px solid #888; */
width: 60%;
max-width: 600px;
text-align: center;
position: relative;
}

/* The Close Button */
.ebcf_close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
position: absolute;
right: 12px;
}

.ebcf_close:hover,
.ebcf_close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}

@media screen and (max-width: 767px){
.ebcf_modal-content {
width: 90%;
text-align: center;
}
.ebcf_modal-content img{
width: 100%;
}
}
</style>


<a href="#" style="display: none" id="mySizeChart">Open Modal</a>


<div id="mySizeChartModal" class="ebcf_modal">

<div class="ebcf_modal-content">
<span class="ebcf_close">&times;</span>
<img src="https://cdn.shopify.com/s/files/1/1302/2643/files/WhatsApp_Image_2023-01-13_at_8.41.29_PM.jpg?v=1673863249" />
</div>

</div>


<script>
// Get the modal
var ebModal = document.getElementById('mySizeChartModal');

// Get the button that opens the modal
var ebBtn = document.getElementById("mySizeChart");

// Get the <span> element that closes the modal
var ebSpan = document.getElementsByClassName("ebcf_close")[0];

// When the user clicks the button, open the modal
ebBtn.onclick = function() {
if(getCookie('popup_removed') != '2'){
$(ebModal).css("display", "flex")
.hide()
.fadeIn();
}else{
console.log('Cookie Present')
}
}

// When the user clicks on <span> (x), close the modal
ebSpan.onclick = function() {
$(ebModal).fadeOut();
if(!getCookie('popup_removed')){
setCookie('popup_removed','1',1)
}else{
setCookie('popup_removed','2',1)
}
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == ebModal) {
$(ebModal).fadeOut();
if(!getCookie('popup_removed')){
setCookie('popup_removed','1',1)
}else{
setCookie('popup_removed','2',1)
}
}
}

window.onload = function(){
setTimeout(()=>{
document.querySelector('#mySizeChart').click()
},2000)
}

function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(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;
}
function eraseCookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
</script>

--

--

Karan Goyal
Karan Goyal

Written by Karan Goyal

Full Stack Developer, Top Rated on Upwork, Worked on 90+ jobs in past 9 months and all jobs are rated 5 stars, coding is fun, and always looking forward to code

No responses yet