mrs68tm
mrs68tm

Reputation: 77

Pop up oncer per session/ visit

I want to use jQuery to add a pop up that appears once per session/visit.

I tried this:

jQuery(document).ready(function($) {  
  setTimeout(function() {
    if (!sessionStorage.previouslyVisited) {
      $('#exampleModalTextLink').modal('show');
      sessionStorage.previouslyVisited = true;
    }
  }
});

in this one, so that the pop up appear once per session (I made it on bootstrap 4)

$(window).load(function(){
  setTimeout(function() {
    $('#exampleModalTextLink').modal('hide');
  }, 15000);
  setTimeout(function(){
    $('#exampleModalTextLink').modal('show');
  }, 2000);
});

Upvotes: 1

Views: 149

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23788

You need to set and get the session variables the following way

jQuery(document).ready(function () {
    // Get saved data from sessionStorage
    var visited = sessionStorage.getItem('visited');

    setTimeout(function () {
        if (visited !== true) {
            $('#exampleModalTextLink').modal('show');
            sessionStorage.setItem('visited', true); // Save data to sessionStorage
        } 
    }, 500);

});

this will check for an existing value if false or null then it will call the modal otherwise won't, I don't know why are you using setTimeout if you are using the document.ready. See here for documentation

Upvotes: 1

Related Questions