skip to Main Content

So I want my page to redirect to a different page when the user refreshes the page. I want the user to be redirected to index.html from gettingstarted.html when he/she refreshes the page.

CODE:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    //meta, seo stuff

    <link rel="stylesheet" type="text/css" href="styles.css">

</head>
<body>

   //body stuff

   //below is just a script that redirects to gettingstarted.html upon first visit

  <script type="text/javascript">

    function redirect(){
    var thecookie = readCookie('doRedirect');
    if(!thecookie){window.location = 'http://domain.tld/gettingstarted.html';
    }}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 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;}window.onload = function(){redirect();createCookie('doRedirect','true','999');}

  </script>

</body>

gettingstarted.html

Pretty much the same thing as index.html

I tried Googling but I kept getting the “Meta Refresh Tag” which in this case, is useless.

2

Answers


  1. Well..You can do it something like this way as :

    if ( document.referrer == null || document.referrer.indexOf(window.location.hostname) < 0 ) {
    sessionStorage.setItem("is_reloaded", true);
    }
    // This one will set the sessionStorage.setItem on first visit
    
    if (sessionStorage.getItem("is_reloaded")) {
    location.href = 'http://domain.tld/gettingstarted.html';
    }
    //Next time then if someone reload the page so it will reload the user to that another link
    

    If the first one doesn’t work so try this one as :

    window.onbeforeunload = function(e) {
      location.href = 'http://domain.tld/gettingstarted.html';
    };
    
    Login or Signup to reply.
  2. This will do what you’re asking, just customize the URL.

    <script>
    if (sessionStorage.getItem('beenhere') ==='1') {
        window.location="http://www.example.com";
    }
    sessionStorage.setItem('beenhere', '1');
    </script>
    

    See:

    See: Check if page gets reloaded or refreshed in Javascript

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search