Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!