Saturday, 10 October 2026

How to show popup once per session on link click

 Learn how to display a custom ad popup only once per session when a user clicks any link on your website using pure HTML, CSS, and JavaScript. This script intercepts the link click, shows the popup, uses sessionStorage to track the user, and smoothly redirects them to their destination.

Complete Code (HTML + CSS + JavaScript)

<!-- CSS Styling for Popup Overlay -->

<style>
#adquashPopupOverlay{
    display:none;
    position:fixed;
    inset:0;
    background:rgba(0,0,0,.75);
    z-index:999999;
    justify-content:center;
    align-items:center;
    padding:15px;
    box-sizing:border-box;
}

#adquashPopup{
    width:100%;
    max-width:500px;
    background:#fff;
    border-radius:14px;
    padding:15px;
    position:relative;
    text-align:center;
    box-shadow:0 10px 30px rgba(0,0,0,.35);
    animation:popupFade .3s ease;
}

#adquashPopup img{
    width:100%;
    display:block;
    border-radius:10px;
}

#adquashClose{
    position:absolute;
    top:20px;
    right:12px;
    width:36px;
    height:36px;
    line-height:36px;
    border-radius:50%;
    background:red;
    color:#fff;
    font-size:24px;
    font-weight:bold;
    cursor:pointer;
    user-select:none;
}

#adquashBtn{
    display:inline-block;
    margin-top:15px;
    padding:12px 24px;
    background:#ff6b00;
    color:#fff;
    text-decoration:none;
    border-radius:8px;
    font-weight:bold;
}

@keyframes popupFade{
    from{opacity:0;transform:scale(.9);}
    to{opacity:1;transform:scale(1);}
}
</style>

<!-- HTML Markup for Popup -->
<div id="adquashPopupOverlay">
    <div id="adquashPopup">
        <div id="adquashClose">&times;</div>
        <a href="https://gridbot.adquash.com/kucoin-dca-bot.html" target="_blank">
            <img src="https://aksharhanumandham.in/ads/kucoin.png" loading="lazy" alt="Advertisement">
        </a>
        <a href="https://gridbot.adquash.com/kucoin-dca-bot.html" target="_blank" id="adquashBtn">
            Visit Now
        </a>
    </div>
</div>

<!-- JavaScript Logic -->
<script>
(function(){
    if(sessionStorage.getItem("adquashPopupShown")){
        return;
    }

    let targetUrl="";
    const overlay=document.getElementById("adquashPopupOverlay");
    const popup=document.getElementById("adquashPopup");

    document.addEventListener("click",function(e){
        const link=e.target.closest("a");
        if(!link) return;

        // Popup ke andar ke links normal chalenge
        if(link.closest("#adquashPopup")) return;

        // Agar pehle hi dikh chuka hai
        if(sessionStorage.getItem("adquashPopupShown")) return;

        e.preventDefault();
        targetUrl=link.href;
        sessionStorage.setItem("adquashPopupShown","1");
        overlay.style.display="flex";
    },true);

    // Close button event
    document.getElementById("adquashClose").onclick=function(){
        overlay.style.display="none";
        if(targetUrl){
            location.href=targetUrl;
        }
    };

    // Outside click event
    overlay.addEventListener("click",function(e){
        if(e.target===overlay){
            overlay.style.display="none";
            if(targetUrl){
                location.href=targetUrl;
            }
        }
    });

    // Popup ke andar click hone par close na ho
    popup.addEventListener("click",function(e){
        e.stopPropagation();
    });
})();
</script>

No comments:

Post a Comment