NodeJS/JS: Scroll to an element after server script finishes

Viewed 26

Here's what I'm trying to achieve: after adding a comment to a web page, you're yanked to the bottom of the page where the newest comment is. As I'm using NodeJS (for server-side writing and reading comments from and to a JSON file), the server script ends with

res.redirect('/' + reqUri + "#comment" + commentIndex);

redirecting to something like /auk.html#comment17. Contrary to what one would expect, the link redirects to /auk.html#comment17 but doesn't scroll there (it scrolls to the top of /auk.html). Why? And how can I make it to scroll to /auk.html#comment17 after running my server script? This seems easy on frontend w/

window.scrollTo(0, document.body.scrollHeight);

but the problem is in timing — it will scroll to the bottom and then immediately (after the server-side finishes) to the top. I know there should be ways to solve this — e.g. sending 418 when the server script finishes and somehow (I don't know much JS) wait for it on the frontend — but I'd better stop here..

0 Answers
Related