0

I'm using Nodejs. First time posting here. Short story, I write a comment in a form. It is submitted it as a POST request. My server processes it. Then it res.redirect(req.get('referer')), aka reloads my page and puts me at the top with my comment posted. Everything works except for keeping my scroll position. I'm using the Handlebars engine which makes adding javascipt tricky. Looking at other threads, none of the proposed solutions work for me. This excerpt of coding shows only the form submitting, and the server responding.

<body>
...
<form id="leavecomment" form method="post" action="/form3/comment/{{video._id}}">

<div class="form-group">     
<textarea input type="text" class="form-control" name="new_comment"></textarea>
</div>
 <button type="submit" class="btn btn-default">Submit</button>
</form>
</body>

on the server:

router.post('/comment/:id', ensureAuthenticated, function(req, res){
...
Form.update(query, form, function(err){
if(err){
console.log(err);
return;
} else {
res.redirect(req.get('referer'));
return;
}
});
}

Once again, everything works except being able to keep my scroll position. I'm wondering if I can get my page to detect a return

I have tried a variety of methods including everything posted on these two threads:MaintainScrollPositionOnPostback property doesn't works with mozilla and Retain scrollbar position even after reloading using javascript Thanks for your help.

Tim
  • 1
  • 2
  • Well, a report, I've made some progress. I managed to put the scroll position into session storage and have it pulled out each time I visit the page. This of course causes problems with other pages that use the same coding, but I probably can get something working from here. – Tim Jun 19 '18 at 21:46
  • I don't know your needs exactly but as a suggestion if I had to scroll up to a specific comment where I've put something I would probably use one anchor that will be returned with the redirect. That would send me directly up to the post that is concerned and have no side effect on other pages. – Christophe Jun 22 '18 at 10:12
  • I'm not sure what you mean Christophe. From what I understand, I post a comment. The server then redirects to 'referer' and then reopens the page, I thought it was a refresh, but it seems instead it's actually going through the get request and opening the page as a new page. So when this is all done, from the user's perspective, he is put at the top of the page instead of where he was scrolled to. Could you describe more what you mean by using an anchor? – Tim Jun 23 '18 at 07:02

0 Answers0