Redirect only once

2

I have an index.html that does an automatic redirection with the window.location function to a different domain. The problem is that if the user gives back to the browser it redirects again, so it is a loop.

How can I do that if a user gives back in the browser does not redirect again? for what I know can only be done by saving a cookie each time and redirect only if the cookie does not exist, is that right?

In this case, how can I do this process and what would the code be?

With this line in the index.html it would be enough for the part of saving a cookie for each user?

document.cookie="cookie1";

Thank you very much.

    
asked by David Matas 18.06.2016 в 00:15
source

3 answers

3

Do the redirect using

window.location.replace("ejemplo.com");

This will make the redirecting page not be saved in the browser history, thus returning the previous url.

Greetings!

    
answered by 18.06.2016 в 03:06
0

You can use the LocalStorage to store a variable, ask if the variable with any indicator already exists. something like this:

if (typeof(Storage) !== "undefined") {
    if(localStorage.getItem("a")){
    // Agregamos el item llamado a
    localStorage.setItem("a", "entro");
   }else{
    //Como si existe el item a, aquí haces tu redirecciòn
    //Agrega tu código de redirección
   }
} else {
    alert("No soporta LocalStorage");
}

Eye: LocalStorage has life time in the browser, I think you should think about that too.

    
answered by 18.06.2016 в 00:48
0

You can use that as Alfonso says:

  if (typeof(wStorage) !== "undefined") {
      if(localStorage.getItem("a")){
      // Agregamos el item llamado a
       localStorage.setItem("a", "entro");
      }else{
       //Como si existe el item a, aquí haces tu redirecciòn
       //Agrega tu código de redirección
       }
      } else {
       alert("No soporta LocalStorage");
    }, 

but it is not necessary to save variant ..

Source: Geek Technology

    
answered by 29.08.2016 в 00:17