15

I am new to jQuery, and I have some needs on my website. I want to show the jQuery div popup at the first time only when the user comes. No need to show again and again.

Still I am using this, but I don't know how to hide at the second time:

var isshow=0;
$(document).ready(function() {
   if (isshow == 0) {
     $('#jPopup').show();
   }
   isshow = 1;
});

But the ishow variable initializes every time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rudrainnovative
  • 223
  • 1
  • 3
  • 9

5 Answers5

27

You can use localstorage. It is easy to understand and use.

$(document).ready(function() {
    var isshow = localStorage.getItem('isshow');
    if (isshow== null) {
        localStorage.setItem('isshow', 1);

        // Show popup here
        $('#jPopup').show();
    }
});

It will show you the popup at first visit of your site.

Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
6

You may use SessionStorage or LocalStorage for this as per your need.

If you need to do only for that session, use SessionStorage. If it should be stored permanently in the user's browser, use LocalStorage.

    $(document).ready(function(){
        if(sessionStorage && !sessionStorage.getItem('isshow')){
            $('#jPopup').show();
            sessionStorage.setItem('isshow', true);
        }
    });
sap
  • 343
  • 3
  • 11
4

You can use localStorage for this purpose as below:

$(document).ready(function(){
   var shown= localStorage.getItem('isshow');
    if(shown !="t"){
        $('#jPopup').show();
        localStorage.setItem('isshow', "t");
    }
});
Pawan
  • 1,704
  • 1
  • 16
  • 37
4

You can set a cookie to store a value and check if it is not set then show popup:

$(document).ready(function() {
    var isshow = $.cookie("isshow");
    if (isshow == null) {
        $.cookie("isshow", 1); // Store

        // Show popup here
        $('#jPopup').show();
    }
});

Or you can set localStorage. Here is a working example. jsFiddle

$(document).ready(function() {
    if(localStorage.getItem('popState') != 'shown') {
        $("#popup").delay(2000).fadeIn();
        localStorage.setItem('popState','shown')
    }
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
2

You need to hide it on load or do it in css (recommended)and then check localstorage to see if it is the first visit.

$(document).ready(function() {
        $('#jPopup').hide(); //hide on load or in css, later check if its the first visit
        var isshow= localStorage.getItem('status');
        //check if it is the first visit
        if (isshow == null || isshow == '') {
            //set variable to 1
            localStorage.setItem('isshow', 1);
            $('#jPopup').show();
        }
        });
Tal
  • 1,091
  • 12
  • 25
  • 1
    @Tal Do you have any style guide links which would advise that? Personally I find it at least mildly annoying to see a big popup appear for a split second as the DOM loads before the script gets executed. – Sebi Aug 22 '15 at 16:43