How to scroll down a web page using pure java script. The page is built dynamically as you scroll down. I have sketched the code below, but there is a problem – document.body.scrollHeight does not change. Those. scrolls once. Therefore, dancing with a delay of 100 milliseconds, I thought that it took time to reload. But even suspending by alert does not help.
function scrollToEndPage (height, attempt) {
alert ("hight:" + height + "scrollHeight:" + document.body.scrollHeight + "att:" + attempt);
if (height! = document.body.scrollHeight)
{
height = document.body.scrollHeight;
window.scrollTo (0, height);
attempt ++;
setTimeout (scrollToEndPage (height, attempt), 100);
}
}
Answer 1, authority 100%
You can do this
var height = 15;
var attempt = 4;
var intS = 0;
function scrollToEndPage () {
console.log ("hight:" + height + "scrollHeight:" + document.body.scrollHeight + "att:" + attempt);
if (height & lt; document.body.scrollHeight)
{
// height = document.body.scrollHeight;
window.scrollTo (0, height);
attempt ++;
height = parseInt (height) + attempt;
}
esle
{
clearInterval (intS);
}
}
intS = setInterval (scrollToEndPage, 100);
Answer 2
It turned out to be implemented through setTimeout as well.
You can’t do this:
setTimeout (scrollToEndPage (height, attempt)}, 100);
Needed:
setTimeout (function () {scrollToEndPage (height, attempt)}, 100);
Answer 3
Use the following code to load and every page refresh:
el.scrollTop = Math.ceil (el.scrollHeight - el.clientHeight);
Where el is a page or element where you want to scroll down everything, and rounding is needed for positioning accuracy on high-resolution screens. scrollHeight is requested every time the page is refreshed.