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

Time:09-16

Almost have it, but might need a hand :

I created a script to redirect AND set a cookie that comes in from ANY page from our site. So even if a user comes in from a direct link to a news article, it will redirect them to our "splash" page and set a "cookie" to avoid further redirects. I have that part working perfect.

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

However, we now want to capture the URL that they came to first and create a variable so we can have them be linked back to that page after they read our SPLASH page.

So in the example above:

Come in via direct link: /news/article1.php No cookie is detected, so we first need to "capture" the page they came in on "$page-refer" and then redirect them to our Splash page.

On the splash page, we would then present a LINK to them with "Proceed to webpage" with the "$page-refer" link.

I did try this ( BELOW ), but this is only grabbing the "google" page and not OUR webpage that they hit first.

Thanks!

$(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;
  }
})

CodePudding user response:

I think you could add the URL as a cookie when you do the redirect, ex:

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

then you could redirect them to the cookie value:

$(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
  }
})

CodePudding user response:

Put together an answer that works great, thanks for the comments and push:

On footer script template:

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

Then on my Splash page:

I captured the variable $pageredirect via $_GET['page-refer'] and then presented that as a link further down the page ( if set )!

  • Related