-1

I have this script that displays a modal if no cookie. the first if, if the cookie exists it hides the modal, and the "ELSE" if there is no cookie, it will display it.

I need to add a document.referer condition as below.

Besides verifying the cookie if it does not exist check the referer (the same function below), and only then display the modal.

The code:

jQuery(document).ready(function() {
  if (jQuery.cookie('visits') > 0.5) {
    jQuery('#active-popup').hide();
    jQuery('#popup-container').hide();
    jQuery('html, body').removeAttr('style');
  } else {
    var pageHeight = jQuery(document).height();
    jQuery('<div id="active-popup"></div>').insertBefore('body');
    jQuery('#active-popup').css("height", pageHeight);
    jQuery('#popup-container').show();
    jQuery('html, body', window.parent.document).css({
      'overflow': 'hidden',
      'height': '100%'
    });
  }

  $(document).ready(function() {
    $('#popup-container').css({
      'left': (Math.floor(Math.random() * 15) + 3) + '%'
    })
    $('#popup-container').css({
      'top': (Math.floor(Math.random() * 23) + 33) + '%'
    })
  });
});

The refer

  if (document.referrer) {
    facebook = /facebook.com/;
    if (facebook.test(document.referrer)) {

    }
  }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I'm not sure what is the question ? Are you just trying to change the "else" in "else if (document.referrer && facebook.test(document.referrer))" ? – Robin D. Aug 23 '16 at 21:08

1 Answers1

2

Change else to else if with the condition you want.

jQuery(document).ready(function() {
  facebook = /facebook\.com/;
  if (jQuery.cookie('visits') > 0.5) {
    jQuery('#active-popup').hide();
    jQuery('#popup-container').hide();
    jQuery('html, body').removeAttr('style');
  } else if (document.referrer && facebook.test(document.referrer)) {
    var pageHeight = jQuery(document).height();
    jQuery('<div id="active-popup"></div>').insertBefore('body');
    jQuery('#active-popup').css("height", pageHeight);
    jQuery('#popup-container').show();
    jQuery('html, body', window.parent.document).css({
      'overflow': 'hidden',
      'height': '100%'
    });
  }

  $(document).ready(function() {
    $('#popup-container').css({
      'left': (Math.floor(Math.random() * 15) + 3) + '%'
    })
    $('#popup-container').css({
      'top': (Math.floor(Math.random() * 23) + 33) + '%'
    })
  });
});
Barmar
  • 741,623
  • 53
  • 500
  • 612