1

I have a script that redirects the user to a new page after X seconds. After being redirected, if the user hits their back button and returns to the page with this script I'd like it if the script does not fire again.

setTimeout(function() {
  window.location.href = "/mypage.html";
}, 3000);
jkupczak
  • 2,891
  • 8
  • 33
  • 55
  • 1
    Duplicate of: http://stackoverflow.com/questions/2498672/detect-if-the-user-has-used-the-back-button?rq=1 – Cerbrus Nov 28 '12 at 14:30
  • Thanks for the link. Seems the answer there is cookies. I was hoping it wouldn't be that complicated. – jkupczak Nov 28 '12 at 19:28

2 Answers2

1

You can get the referrer property in JavaScript like this:

var referrer_url = document.referrer;
document.write("You come from this url: " +referrer_url);

Then, just wrap your setTimeout() with a conditional check to see which URL the person is coming from and do (or do not) do the redirect depending on where they came from.

JasCav
  • 34,458
  • 20
  • 113
  • 170
  • This works if I use a link to navigate from page A to page B (B being the page with the script.) However, going from B to C (C being the page I am using the javascript on B to redirect them too) and then using the browser's back button to get back to B does NOT work. No referrer is output by the script. – jkupczak Nov 28 '12 at 15:41
  • @jimmykup - Ah...completely missed you using the back button. My apologies. I'll leave this answer in case it helps someone else in the future. – JasCav Nov 28 '12 at 21:22
0

I used the link Cerbrus provided and went the cookie route to solve this. More complicated than I would have liked but it got the job done.

This script will redirect the user to a new page after 3 seconds. It will first check if a cookie exists, and if it does it will not redirect. If there's no cookie, it will create a cookie and then redirect the user. If the user hits the back button the script will find the cookie that was created and it will prevent the script from redirecting the user again.

// Function to create a new cookie
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

// Function to read a cookie
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Use the readCookie function to assign the cookie to a variable (if it's available)
var currentcookie = readCookie('mycookie');

// If/else statement to fire javascript if the cookie is not present
if (currentcookie) {
    // do nothing since the cookie exists
}
else {
    // Cookie doesn't exist, so lets do our redirect and create the cookie to prevent future redirects

    // Create a cookie
    createCookie('mycookie','true');

            // Perform the redirect after 3 seconds
    setTimeout(function() {
      window.location.href = "/mypage.html";
    }, 3000);
}
jkupczak
  • 2,891
  • 8
  • 33
  • 55