Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Save and refresh language preferences stored in localstorage javascript

Hi i got a bit of the problem trying to implement language switcher/preference

I made a localstorage item for that, but got a problem becuse of reload need.

function lang_displayed_settings() {
  if (localStorage.getItem("language") === "pl") {
    window.location.hash = "#pl"
    location.reload();
  }
}

I tried to do a function on window load to get a item with preferenced language but i cant really make it to be reload automaticly. Or im getting a loop when window is just reloading on load. I need a function which works on hrefs etc.

Now i have no idea what i can change to get it works! Hope someone made it better than me ;d

(code from above is a clear one, before me useless trying)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If the language item in the localstorage is already set to "pl", your code will reload and will find the property set to the same value again and will just keep reloading your page. The solution is to add another if condition to check for the existence of #pl in window.location.hash so you wouldn't have to reload the page when the hash is already set:

function lang_displayed_settings() {
  if (localStorage.getItem("language") === "pl") {
    if (window.location.hash === "#pl") {
      return;
    } else {
      window.location.hash = "#pl";
      return location.reload();
    }
  }
}

If you want the language setting to support many languages, you can load it from the localsorage on the first loading of the page and then run the hashing logic as follows:

window.onload(() => {
    const lang = localStorage.getItem("language")
    if (window.location.hash === `#${lang}`) {
        return;
      } else {
        window.location.hash = `#${lang}`;
        return location.reload();
      }
})

If my answer was helpful an upvote would be appreciated.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda