detect the browser language with js and redirect the visitor

1

Hello I need that when a user opens my website, detect the language and redirect it to the browser language. I'm trying this but it goes into a loop and the page loads constantly. I hope you can help. There are 3 languages that I have to address: German, Spanish and English.

window.onload = function () {

   var ln = window.navigator.language || navigator.browserLanguage;

   if (ln == 'de') {

     window.location.href = 'indexDe.html';

   } else if (ln == 'es') {

     window.location.href = 'indexEs.html';

   }más{

     window.location.href = 'indexEn.html';

   }

}
    
asked by Lucianosainz 25.11.2018 в 19:46
source

2 answers

0

The following response applies if all of your index files are performing this same verification.

The problem is that you are always checking the language and redirecting it without verifying if the user is already on the page corresponding to your language. As a result, an infinite loop of redirection is created. To solve this, you can get the pathname of the page you are on and verify that it is not the same one you are redirecting to.

You can do it in the following way:

    window.onload = function () {

   var ln = navigator.language || navigator.userLanguage;
   /*Validar que no se encuentre en la pagina correspondiente a su idioma*/
   let pagActual = window.location.pathname;
   
   
   if (ln == 'en-EN' && !pagActual.includes("indexEn")) {
       window.location.href = 'indexEn.html';  
   } else if (ln == 'es-ES' && !pagActual.includes("indexEs")) {
       window.location.href = 'indexEs.html';
   } else if (ln == 'de-DE' && !pagActual.includes("indexDe")){
       window.location.href = 'indexDe.html';
   } else{
     console.log("Otro idioma");
   }

}
    
answered by 25.11.2018 / 20:20
source
0

It seems to me that your code should work like this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script>
    window.onload = function () {

   var ln = navigator.language || navigator.userLanguage;
   if (ln == 'en') {

     console.log("english")
     window.location.replace("indexEN.html");

   } else if (ln == 'es-419') {

     console.log("español")
     window.location.replace("indexES.html");
   }else if (ln == 'de-DE'){
     console.log("alemán")
     window.location.replace("indexDE.html");
   }else{
     console.log("desconocido")
   }

}
</script>
</body>
</html>

OBSERVATIONS

  • To obtain the browser language it would be: var ln = navigator.language || navigator.userLanguage;
  • For the case of the English language if it is en
  • In the case of Spanish, the label should be: es-419
  • For the case of the German language it should be: de-DE
  • When using window.onload this code will start working as soon as the page loads, so I think it should only go in one that redigija to any other
  • What happens is that for each language there are variants

    In addition to the above you should use window.location.replace which replaces the current resource with one passed as parameter

    window.location.replace("https://www.es.stackoverflow.com");
    
      

    Try using it because it does not keep the pages in history

        
    answered by 25.11.2018 в 20:02