【问题标题】:Show pop-up message to users coming from redirected URLs向来自重定向 URL 的用户显示弹出消息
【发布时间】:2019-01-02 02:11:45
【问题描述】:

我想向来自 301 重定向域(我们称之为 previous.com)的网站访问者显示弹出消息

想法是有几个旧网址:

  • previous.com/contact
  • previous.com/news
  • previous.com/test

他们会重定向到新的 URL:

  • new.com/contact
  • new.com/news
  • new.com/test

要求是在这些用户通过重定向的 URL 访问网站时向他们显示弹出消息

最好用 JavaScript 来构建它吗?

<script language="JavaScript">

function checkRef() {
  if (document.referrer.indexOf('previous.com') > -1) {
    window.open('pop-up.html',"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=450,left=30,top=80");

  }
  else  {

  }
}

</script>

【问题讨论】:

  • 我认为referrer 不会包含previous.com - 很可能它会包含指向previous.com 的站点。也许您需要进行重定向以在 URL 中包含查询参数。

标签: javascript redirect http-redirect


【解决方案1】:

这对我有用:

function checkRef() {

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("closeBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

  if (document.referrer.indexOf('previous.com') > -1) {
    modal.style.display = "block";

	// When the user clicks on <span> (x), close the modal
		span.onclick = function() {
    		modal.style.display = "none";
		}

	// When the user clicks on the button, close the modal 
		btn.onclick = function() {
    		modal.style.display = "none";
		}

	// When the user clicks anywhere outside of the modal, close it
		window.onclick = function(event) {
    		if (event.target == modal) {
        		modal.style.display = "none";
    		}
		}

  	}
  		else  {
	  		console.log('Not from previous.com');
  		}
	}
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 10px;
    border: 1px solid #888;
    width: 25%; /* Could be more or less, depending on screen size */
}
.content {padding:8px; border:1px solid #cccccc; text-align: center;}
.content h2 {      font-size: 1rem;    padding-left: 12%;    padding-right: 12%;    font-weight: normal;}
.content p {padding-left: 15px; padding-right: 15px;}

/* The Close Button */
.close {
    color: #333;
    float: right;
    font-size: 18px;
    font-weight: bold;
    background-color: #ccc;
    border: 1px solid #aaa;
    width: 20px;
    height: 20px;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
button {    background-color: #5e8f40;
    color: #ffffff;
    padding: 15px;
    border: 0 none;
    padding-top: 10px;
    padding-bottom: 10px;
    padding-left: 30px;
    padding-right: 30px;}
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
  	<div class="content">
	    <span class="close">&times;</span>
	    <p>Some text here...</p>
	    <button id="closeBtn" onclick="setCookie('clicklink', 'yes', 30)">Close</button>
	</div>
  </div>

</div>

【讨论】:

    猜你喜欢
    • 2017-10-29
    • 2019-10-17
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多