Skip to content

Instantly share code, notes, and snippets.

@webpapaya
Created December 1, 2020 20:19
Show Gist options
  • Save webpapaya/f97f430b7c4f2c894f68644d2cd5ced5 to your computer and use it in GitHub Desktop.
Save webpapaya/f97f430b7c4f2c894f68644d2cd5ced5 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>FHS-Modules</title>
</head>
<body>
<a href="/test1">First Link</a>
<a href="/test2">Second Link</a>
<section id="content">
</section>
<script src="./index.js" type="module"></script>
</body>
</html>
const onRouteChange = () => {
const pathname = new URL(window.location).pathname;
const domElement = document.querySelector('#content')
if (pathname === '/test1') {
domElement.innerHTML = 'test1'
} else if (pathname === '/test2') {
domElement.innerHTML = 'test2'
} else {
domElement.innerHTML = 'not found =('
}
}
onRouteChange() // call on page load
Array.from(document.querySelectorAll('a')).forEach((link) => {
document.addEventListener('click', (evt) => {
evt.preventDefault()
history.pushState(null, "My new page", evt.target.href)
onRouteChange()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment