my-portfolio/src/blog/index.js

41 lines
937 B
JavaScript
Raw Normal View History

document.addEventListener('DOMContentLoaded', () =>
{
goToURL(window.location.pathname);
});
window.addEventListener('popstate', e =>
{
goToURL(window.history.state);
});
/**
* goToURL tries to go to the specified URL if not shows the error page (not yet implemented)
* @param {string} url of the page
*/
function goToURL(url)
{
// Get the current URL and split it into an array
let urlArray = url.split('/');
let newUrl = "";
if (urlArray.includes('blog'))
{
newUrl = url;
}
// Check if the URL is a post page
if (urlArray[0] === 'post')
{
// Create a new URL with the dynamic part
newUrl = "/blog/" + url;
}
// Update the URL in the browser without reloading the page
window.history.pushState(null, null, newUrl);
// Get the dynamic part of the URL
document.querySelector("#url").innerHTML = decodeURI(urlArray[urlArray.length - 1]);
}