One option would be to write a function that gathers the necessary height information. Then run the function to initialize your height variable(s). If something is hidden after the initialization, run the function again to update the variable(s).
You could also check to see if there's an event listener built into JavaScript for detecting changes to the page height. If something like that is available, you could have that run the necessary code to update the height information.
For example, the "resize" event listener might be useful when gather height information when the user changes their browser's window size. More information can be found here: https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event
Additional listeners may be available for other events that impact page height.