Run Javascript when updating the page

Asked

Viewed 201 times

-2

I have an Html5 application, with a single Javascript function, which keeps the scroll always at the end of the page, in this HTML, I have an update tag where I can set the automatic update time, this page will load a txt text file that is updated by an external source every 10 seconds, I wonder if it is possible to run a script whenever the page is updated?

1 answer

1


try this:

window.onload = function() {
    setTimeout(function () {
        window.scrollTo(0, 1000);
        window.location.reload(1);
    }, 1000);
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "arquivo.txt", true);
    xhttp.responseType = "text";
    xhttp.onreadystatecange = function() {
        if (xhttp.readyState ==4 && xhttp.status == 200) {
            var conteúdo = xhttp.responseText;
            var resposta = xhttp.response;
            document.writeln("variável conteúdo: " + conteúdo);

            document.writeln("variável resposta: " + resposta);
        }
    };
    xhttp.onerror = function() {
        document.writeln("<H1>Error HTTP_ERROR_" + xhttp.status + " bad request.</H1>"); // Se der erro, ele vai mostrar um aviso tipo 404...
    };
};

This code does not work on local files, just on Webserver.

  • To avoid long discussions in the comments; your conversation was moved to the chat, to proceed just click on the link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.