Window.location.reload() on specific position

I have form on my personal page, when you submit it, I call JS function to reload page and show alter box.

window.onload = function() {
  var reloading = sessionStorage.getItem("reloading");
  if (reloading) {
      sessionStorage.removeItem("reloading");
      sended();
  }


function reloadP() {
    sessionStorage.setItem("reloading", "true");
    window.location.reload();
}
function sended(){
$('#alterbox').fadeIn('slow').delay(1000).fadeOut('slow').hide(0);
}

Problem is, that page reloads and shows header, but contact form is way too below, on the bottom of the page. Can I reload page to stay on same place?

Upvotes: 0

Views: 1521

Answers (1)

Dmitry Sobolevsky
Dmitry Sobolevsky

Reputation: 1191

Before reload you can save scroll position in sessionStorage, too. And then after reload get it, and scroll your page to them.

Before reload:

sessionStorage.setItem("scroll", window.pageYOffset);

After reload:

const scrollBy = sessionStorage.getItem("scroll")    
if (scrollBy) {
 window.scrollTo(scrollBy,0)
}

Upvotes: 2

Related Questions