Loading a page with xhttp

I have a little question that I want to ask.

First of all, I'm using JS for loading a page.

function ReLoad() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function () {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
             document.getElementById("box").innerHTML = xhttp.responseText;
          }
       };
      xhttp.open("GET", "calc/index.html", true);
      xhttp.send();
 }

Now, My problem is while I'm inserting this index into the main page, because the page auto-scroll immediately to the start and I want to load the page without this annoying effect.

Please, Can someone help me? Thanks

Answers


you can store the scroll position;

var currentPosition = window.pageYOffset || document.documentElement.scrollTop;

and reapply it:

window.scrollTo(0, currentPosition);

so your code would become:

function ReLoad() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
     if (xhttp.readyState == 4 && xhttp.status == 200) {
         var currentPosition = window.pageYOffset || document.documentElement.scrollTop;
         document.getElementById("box").innerHTML = xhttp.responseText;
         window.scrollTo(0, currentPosition);
      }
   };
  xhttp.open("GET", "calc/index.html", true);
  xhttp.send();
}

I finally solved it adding this to the end of the code:

e.preventDefault();
return false;

Need Your Help

ADB stucks on my Android smart phone

android eclipse debugging adb galaxy

After connecting my device to my real device (Galaxy S / Android 2.3.3) and lauching the DDMS Heap tab, sometimes the device disconnects and is no longer available as vaild Android target for the A...

Can I pass an IO port as a parameter to a function?

c embedded

I try to make function that take IO register parameter (AVR 32 PORTA) to manipulate it .but it doesn't work .