This repository has been archived on 2025-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
coryd.dev-eleventy/src/assets/scripts/paginator.js
2024-04-04 14:12:12 -07:00

25 lines
No EOL
968 B
JavaScript

window.onload = () => {
const pagination = document.getElementById('pagination')
const uri = window.location.pathname
const urlSegments = uri.split('/').filter(segment => segment !== '')
let pageNumber = parseInt(urlSegments[urlSegments.length - 1]) || 0
pagination.querySelector(`option[value="${pageNumber.toString()}"]`).setAttribute('selected', 'selected')
if (pagination) {
pagination.addEventListener('change', (event) => {
pageNumber = event.target.value
if (urlSegments.length === 0 || isNaN(urlSegments[urlSegments.length - 1])) {
urlSegments.push(pageNumber.toString())
} else {
urlSegments[urlSegments.length - 1] = pageNumber.toString()
}
if (pageNumber === 0) {
window.location.href = `${window.location.protocol}//${window.location.host}/`
} else {
window.location = `${window.location.protocol}//${window.location.host}/${urlSegments.join('/')}`
}
})
}
}