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

180
Visualizações
Hash based Dom manuplation using Javascript

I have a hash based url (e.g. www.dummy.com#/hello/welcome), I want to trigger javascript code when /welcome changes to /start (e.g. www.dummy.com#/hello/start).

Following is the code I've tried but isn't working.

window.addEventListener("hashchange",function() {
alert('The hash has changed!');
},false);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The hashchange event only happens when the hash changes, so if you just type the URL and hit enter nothing happens. Therefore you need to handle more cases: 1) the hash changes, 2) user types URL and 3) the history forward/back button is used to navigate to the page.

const eitherhashorload = e => {
  let hash = location.hash.substring(1);
  if(!hash){
    location.hash = 'welcome';
    return;
  }
  console.log(hash);
};

window.addEventListener("hashchange", eitherhashorload);

document.addEventListener('DOMContentLoaded', eitherhashorload);
<p><a href="#start">Start</a> - <a href="#end">End</a></p>

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