Stop 2nd popup from opening if 1st popup already opened

Hi all

I am using magnific popup script to open two popups.

First popup is opening on click of a link.

Second popup is opening automatically on page load after 5 second interval.

Now i want is, if First magnific Popup is already opened, then second magnific popup should not open till first popup is opened.

How can i do it ??

here is a codepen
Codepen link

Thanks
Vineet

no suggestions for this ??

Oh yes, there are suggestions for this. Doing something about it is a different story.

Magnific popup has an instance object that contains an items array, which has each of the popups in there so you can check if its empty before showing a popup.

You can see an example of the instance and array at https://github.com/dimsemenov/Magnific-Popup/issues/53

So far as getting that to work with your existing code, that will have to wait a while for me.

tl;dr: patience please.

Assign setTimeout to a variable, then you can cancel it if the link is clicked first:

var popup = setTimeout(function() {
  $.magnificPopup.open({
    items: {
      src: '#myModal'
    },
    type: 'inline'
  });
}, 3000);

$('.open-popup-link').magnificPopup({
  type: 'inline',
  midClick: true
});

$(".open-popup-link").on("click", function() {
  clearTimeout(popup);
});

Okay - you can cache the magnificPopup instance to a local variable mfp, and then check if it’s not open, with:

if (!mfp.isOpen) {

The only two changes here to your existing code, are the var mfp line, and the mfp.isOpen line.

$(document).ready(function () {
  var mfp = $.magnificPopup.instance;
  setTimeout(function() {
    if (!mfp.isOpen) {
      $.magnificPopup.open({
        items: {
          src: '#myModal' 
        },
        type: 'inline'
      });
    }
  }, 5000);
});
1 Like

Thanks Pullo, Your solution worked perfect.

Thanks Paul_wilkins, Your solution also worked perfect.

so both solutions worked perfect for me.

Thanks again for helping me in solving it.

vineet

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.