向来自重定向URL的用户显示弹出消息

时间:2018-07-25 09:47:06

标签: javascript redirect http-redirect

我想向301重定向域中的站点访问者显示弹出消息(我们称其为previous.com)

想法是有几个旧的URL:

  • 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>

1 个答案:

答案 0 :(得分:0)

这对我有用:

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>

相关问题