Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show jQuery popup only at first visit of a page

Tags:

jquery

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.

like image 753
rudrainnovative Avatar asked Aug 22 '15 10:08

rudrainnovative


People also ask

How do you display a load pop only once per user session?

click(function(e) // You are clicking the close button { $j('#popup'). fadeOut(); // Now the pop up is hiden. }); $j('#popup'). click(function(e) { $j('#popup'). fadeOut(); }); });

How do I show popups in jQuery?

To create a popup, add the data-role="popup" attribute to a div with the popup contents. Then create a link with the href set to the id of the popup div, and add the attribute data-rel="popup" to tell the framework to open the popup when the link is tapped. A popup div has to be nested inside the same page as the link.

How do I show a pop up on page load?

Answer: Use the Bootstrap . modal('show') method You can use the Bootstrap . modal('show') method for launching the modal window automatically when page load without clicking anything.


5 Answers

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.

like image 197
Umesh Sehta Avatar answered Sep 19 '22 05:09

Umesh Sehta


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);
        }
    });
like image 39
sap Avatar answered Sep 18 '22 05:09

sap


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.

like image 31
Mohit Kumar Avatar answered Sep 21 '22 05:09

Mohit Kumar


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");
    }
});
like image 28
Pawan Avatar answered Sep 20 '22 05:09

Pawan


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')
    }
});
like image 43
Manoj Dhiman Avatar answered Sep 22 '22 05:09

Manoj Dhiman