Avoid the browser's autoscroll when updating a div

0

I am implementing a dynamic content website. In particular, I have a "div" which I will update its content in the following way:

This is the "div":

<div id="informationONDevices"></div>

I proceed to delete its contents:

document.getElementById("informationONDevices").innerHTML = "";

Finally, I'm putting "divs" (the code that goes next is inside a loop):

var element  = document.createElement("div");

element.innerHTML = "<h3>Dades de la sonda amb ID: " + this.responseXML.getElementsByTagName('addONDevices')[count1].childNodes[0].nodeValue + " i Alias: " + this.responseXML.getElementsByTagName('aliasONDevices')[count1].childNodes[0].nodeValue + "</h3>" + "<br><br>"
+ "<span class='title'>Actual</span><br>";

document.getElementById("informationONDevices").appendChild(element);

The problem is that every time you update the content of the "div" the page does autoscroll and does not stay where it was. Any solution? I've been searching and trying a lot of things and none of them has helped me.

Thank you very much!

    
asked by Kane12 09.06.2018 в 01:44
source

1 answer

0

After spending hours looking for information, I decided to try things out.

Here the solution:

var oldPositionSB = window.pageYOffset;

document.getElementById("informationONDevices").innerHTML = "";

…

document.getElementById("informationONDevices").appendChild(element);

window.scrollTo(0, window.pageYOffset + (oldPositionSB - window.pageYOffset));
    
answered by 09.06.2018 в 15:22