如何在变量中捕获引用 URL 然后重定向到新页面 jquery 或 javascript

How to capture the referring URL in a variable and then redirect to new page jquery or javascript

差不多有了,但可能需要帮助:

我创建了一个脚本来重定向和设置来自我们网站任何页面的 cookie。因此,即使用户从直接 link 进入新闻文章,它也会将他们重定向到我们的“splash”页面并设置“cookie”以避免进一步重定向。我的那部分工作完美。

$(document).ready(function() {
  if (typeof Cookies.get('secondvisit') === 'undefined') {
    window.location.href = "/index-donate.php";
  }
})

但是,我们现在想要捕获他们首先访问的 URL 并创建一个变量,以便我们可以让他们在阅读我们的 SPLASH 页面后 link 返回到该页面。

所以在上面的例子中:

通过直接进入 link:/news/article1。php 没有检测到 cookie,所以我们首先需要“捕获”他们在“$page-refer”上进入的页面,然后将他们重定向到我们的 Splash 页面。

在初始页面上,我们会向他们展示一个 LINK 并带有“$page-refer” link。

我试过这个(下面),但这只是抓取了“google”页面,而不是他们首先点击的我们的网页。

谢谢!

$(document).ready(function() {
  if (typeof Cookies.get('secondvisit') === 'undefined') {
    var referringURL = document.referrer;
    var local = referringURL.substring(referringURL.indexOf("?"), referringURL.length);
    location.href = "/index-donate.php" + local;
  }
})

我认为您可以在进行重定向时将 URL 添加为 cookie,例如:

$(document).ready(function () {
  if (typeof Cookies.get('secondvisit') === 'undefined') {
    Cookies.set('initialvisitedpage', window.location.href);
    window.location.href = "/index-donate.php";
  }
});

然后您可以将它们重定向到 cookie 值:

$(document).ready(function() {
  if (typeof Cookies.get('secondvisit') === 'undefined') {
    window.location.href = Cookies.get('initialvisitedpage') || '/'; // or whatever this could default to in case they don't have the cookie
  }
})

整理出一个效果很好的答案,感谢评论和推送:

在页脚脚本模板上:

$(document).ready(function () {
         if (typeof Cookies.get('secondvisit') === 'undefined') {
             location.href = "/index-donate.php?page-refer=" + encodeURIComponent(location.href);         
      }
   })

然后在我的启动页面上:

我捕获了变量 $pageredirect via $_GET['page-refer'],然后将其显示为页面下方的 link(如果已设置)!