Created
December 1, 2020 20:19
-
-
Save webpapaya/f97f430b7c4f2c894f68644d2cd5ced5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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