如果 referrer 是 Facebook,则显示 Facebook like box popup

Show Facebook like box popup if referrer is Facebook

我在弹出窗口中有一个 Facebook 点赞框 div。但是,我希望仅当用户直接从 Facebook 进入网站时才显示 Facebook Like Box。

简而言之:

用户来自 Facebook:显示

用户直接输入URL:不显示

用户来自google:不显示


我如何检查和验证 facebook 是用户引荐来源网址?这是我的代码:

if (document.cookie.indexOf('_visited=1') == -1) {
  var delay_popup = 1000;
  setTimeout("document.getElementById('parent_popup').style.display='block'", delay_popup);
  var date = new Date;
  date.setDate(date.getDate() + 1); // текущая дата + 1 день
  document.cookie = '_visited=1; path=/; expires=' + date.toUTCString();
}
#parent_popup {
  background-color: rgba(0, 0, 0, 0.8);
  display: none;
  position: fixed;
  z-index: 99999;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

#popup {
  background: #fff;
  width: 380px;
  margin: 10% auto;
  padding: 5px 20px 13px 20px;
  border: 10px solid #ddd;
  position: relative;
  -webkit-box-shadow: 0px 0px 20px #000;
  -moz-box-shadow: 0px 0px 20px #000;
  box-shadow: 0px 0px 20px #000;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  border-radius: 10px;
}

#popup h4 {
  font: 28px Monotype Corsiva, Arial;
  font-weight: bold;
  text-align: center;
  color: #008000;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .3);
}

#popup h5 {
  font: 24px Monotype Corsiva, Arial;
  color: red;
  text-align: center;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .3);
}

.close {
  background-color: rgba(0, 0, 0, 0.8);
  border: 2px solid #ccc;
  height: 24px;
  line-height: 24px;
  position: absolute;
  right: -24px;
  cursor: pointer;
  font-weight: bold;
  text-align: center;
  text-decoration: none;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  text-shadow: 0 -1px rgba(0, 0, 0, 0.9);
  top: -24px;
  width: 24px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  -ms-border-radius: 15px;
  -o-border-radius: 15px;
  border-radius: 15px;
  -moz-box-shadow: 1px 1px 3px #000;
  -webkit-box-shadow: 1px 1px 3px #000;
  box-shadow: 1px 1px 3px #000;
}

.close:hover {
  background-color: rgba(0, 122, 200, 0.8);
}
<div id="parent_popup">
  <div id="popup">
    <font size="2" style="font-size: 15pt;">
      <b style="color: rgb(255, 0, 0);"><center>pls like us !</center></b>
    </font>
    <center>like box code </center>
    <p style="text-align: center;">
      <strong><a class="button" href=""></a></strong>
    </p>
  </div>
</div>

您可以使用 JavaScript 检测引荐来源网址,然后在引荐来源网址 URL 与 facebook 使用 .match(regex):

匹配时显示元素

var fblike = document.getElementById('fblike');
var ref = document.referrer;
if (ref.match(/^https?:\/\/([^\/]+\.)?facebook\.com(\/|$)/i)) {
  fblike.style.display = 'block';
}
#parent_popup {
  background-color: rgba(0, 0, 0, 0.8);
  position: fixed; z-index: 1;
  top: 0; right: 0; bottom: 0; left: 0;
  text-align: center;
}

#popup {
  background: #fff;
  margin: 10% auto;
  width:50%;
  padding: 5px 20px 13px 20px;
  border: 10px solid #ddd;
  position: relative;
  border-radius: 10px;
}

#fblike {
  display: none;
}
<div id="parent_popup">
  <div id="popup">
    <div id="fblike"><p>Like us on Facebook!</p></div>
  </div>
</div>

请注意 #fblike 默认情况下如何具有 display:none。当用户从 facebook 访问时,div 将使用纯 JavaScript 代码显示:document.getElementById('fblike').style.display = 'block';

Referer 正则表达式代码来自:Checking the referrer with JavaScript