📝 Added sandbox (code sketchbook)
This commit is contained in:
parent
b475595f7f
commit
b5daf18b1a
142 changed files with 100702 additions and 0 deletions
28
sandbox/vanilla_spa/app.js
Normal file
28
sandbox/vanilla_spa/app.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* https://medium.com/altcampus/implementing-simple-spa-routing-using-vanilla-javascript-53abe399bf3c */
|
||||
|
||||
const about = '<h1>I am About Page</h1>'
|
||||
const contact = '<h1>I am Contact Page</h1>'
|
||||
const home = '<h1>I am Home Page</h1>'
|
||||
|
||||
const routes = {
|
||||
'/' : home,
|
||||
'/contact' : contact,
|
||||
'/about' : about
|
||||
}
|
||||
|
||||
const rootDiv = document.getElementById('root')
|
||||
console.log(window.location.pathname)
|
||||
rootDiv.innerHTML = routes[window.location.pathname]
|
||||
|
||||
const onNavigate = (pathname) => {
|
||||
window.history.pushState(
|
||||
{},
|
||||
pathname,
|
||||
window.location.origin + pathname
|
||||
)
|
||||
rootDiv.innerHTML = routes[pathname]
|
||||
}
|
||||
|
||||
window.popstate = () => {
|
||||
rootDiv.innerHTML = routes[window.location.pathname]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue