javascript – Page down when restarting with JS

Question:

She was hoping that when the page loaded, it would automatically go to the bottom of it. I don't want to use jquery as the project is relatively simple. It is not necessary. I used the following code:

<body onload="toBottom();">

</body>
function toBottom(){
  var elem = document.querySelector("body");
  elem.scrollTop = elem.scrollHeight;
}

It works when page loads, ok! But when I refresh, no. Only if I close the page, and enter again. How to solve this?

Answer:

That way I tested here giving F5 and reload on the page and it works perfectly.

I edited it with the function cria br to keep 30 thousand brs on the page kk

window.onload = function() {
  criaBr();

  toBottom();
};

function criaBr(){
  var body = document.querySelector("body");
  var br = "<br/>";
  for(var i = 0; i < 10; i++){
      br += br;
  }
  
  body.innerHTML += (br + "<button onclick='refresh()'> Refresh </button>");
}

function refresh(){
	location.reload();
}

function toBottom(){
  var elem = document.querySelector("body");
  elem.scrollTop = elem.scrollHeight;
}
<body>
b


</body>
Scroll to Top