I want to execute this script within the astro framework to adjust the placement of my footer at the end of the page if the page is shorter than the viewport (if there is a better way tell me, but it’s not the problem here):
window.onload = function () {
const footer = document.querySelector('footer')
if (!footer) return
const bodyHeight = document.body.clientHeight
const windowHeight = window.innerHeight
if (bodyHeight < windowHeight) {
footer.style.position = 'absolute'
footer.style.bottom = '0'
footer.style.width = '100%'
}
}
But because it’s asynchronous document.body.clientHeight
is equal to 0 9 times on 10 and window.onload
handler change nothing. I also tried body.onload
but same thing.
Here is my crappy hotfix:
window.onload = function () {
const footer = document.querySelector('footer')
if (!footer) return
footer.style.display = 'none'
setTimeout(function () {
const bodyHeight = document.body.clientHeight
const windowHeight = window.innerHeight
if (bodyHeight < windowHeight) {
footer.style.position = 'absolute'
footer.style.bottom = '0'
footer.style.width = '100%'
}
footer.style.display = 'block'
}, 100) // Let the page load first
}
3
Answers
You don’t need to use JavaScript to do this effect.
Take a look at this for example:
No sure if it will resolve your issue, but I would first try to use this event listener instead of window.onload:
I don’t know what your CSS looks like but for the JS part:
You are using
setTimeout
to delay the execution of your logic/function.I would recommend using
Promise.resolve()
.It is similar idea but little bit more elegant since rather than waiting for arbitrary time set in your
setTimout
function, it is scheduled to run in the next cycle of the event loop, after all the other functions on the stack trace have returned.Your code would look like this:
Give it a go!