Isabela Cristina Isabela Cristina - 3 months ago 24
Javascript Question

include in document.referrer in "else if"

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)) {

}
}

Answer

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.referer && facebook.test(document.referer)) {
    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) + '%'
    })
  });
});