Popup on website load once per session

32,229

I know I shouldn't really do this and provide you with the answer without you trying to attempt it first because you won't be learning that way.

But I'm feeling nice today, so I'm providing you of a way of doing a pop up on first load and not loading the pop again until the session been destroyed.

You need to set the session, when you load the page by doing the following:

sessionStorage.setItem('firstVisit', '1');

Then you just need to check for the session like this:

If there is no session called firstVisit then show message box

    if (!sessionStorage.getItem('firstVisit') === "1")
    {
       $(".message").show(); 
    } 

EXAMPLE

HTML

<div class="message">
    <div class="message_pad">
        <div id="message"></div>
        <div class="message_leave">

        </div>
    </div>
</div>

JavaScript/JQuery

// Save data to sessionStorage
sessionStorage.setItem('firstVisit', '1');

/* Fix size on document ready.*/
$(function()
{
    if (!sessionStorage.getItem('firstVisit') === "1")
    {
       $(".message").show(); 
    } 

    //Close element.
    $(".message").click(function()
    {
       $(this).hide();
    });

    $(".message").css({
        'height': $(document).height()+'px'
    });
    $(".message_pad").css({
        'left': ($(document).width()/2 - 500/2)+'px'
    });
});
/*
* Fix size on resize.
*/
$(window).resize(function(){
    $(".message").css({
        'height': $(document).height()+'px'
    });
    $(".message_pad").css({
        'left': ($(document).width()/2 - 500/2)+'px'
    });
});

JSFIDDLE

Share:
32,229
Admin
Author by

Admin

Updated on January 04, 2020

Comments

  • Admin
    Admin over 4 years

    I found some ways to make javascript/jquery popup windows. But, there were some problems, first of all, I am not very good with these two languages, just beginner.

    If there is any ready code, for opening POPUP window on website load, but only once per browser session. It would be very nice, if someone could help me with this.

    I need simple popup, with just some text in it, but design for popup box something good looking, not like original browser popup (like in image), of course, with closing button.

    http://i.stack.imgur.com/xNWxf.png

    Thanks a lot

  • Admin
    Admin over 8 years
    Thanks you a lot, it helped me. I made some changes, because it dosn't work at first, but then it did. // Save data to sessionStorage if(!sessionStorage.getItem('firstVisit')){ sessionStorage.setItem('firstVisit', '1'); }else{ sessionStorage.setItem('firstVisit', '0'); } /* Fix size on document ready.*/ $(function(){ if (sessionStorage.getItem('firstVisit') === "1"){ $(".message").css('display', 'inline') } // Aizver POPUP $("#message").click(function(){ $(".message").hide(); }); });
  • JGallardo
    JGallardo about 6 years
    This is useful but perhaps better as a comment