summaryrefslogtreecommitdiff
path: root/script.js
blob: b37a235f77742b03197e0ec15330b9db87c799ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const routes = {
  '/':      'page-home',
  '/blog':  'page-blog',
  '/about': 'page-about',
};

function navigate() {
  const hash = location.hash.replace('#', '') || '/';

  // Hide all pages
  document.querySelectorAll('.page').forEach(el => el.classList.remove('active'));

  // Update nav
  document.querySelectorAll('nav a').forEach(a => {
    const path = a.getAttribute('href').replace('#', '');
    a.classList.toggle('active', path === hash);
  });

  // Show matching page, fall back to 404
  const pageId = routes[hash] || 'page-404';
  document.getElementById(pageId).classList.add('active');
}

window.addEventListener('hashchange', navigate);
navigate(); // run on load