Dynamically change text on HTML page from the С program

192 views Asked by At

I have a web server (thttpd), and I want to periodically change the text on the HTML page depending on the data from my application C. I think to write a simple .txt file in the C application and periodically read it on a web server and change data on the HTML page. But I don't know how to do it. Maybe there are more correct and simple versions, how to do it?

ANSWER: I make this by using fetch(). This code read text from file dynamic_data.txt and change element with id="ispr_1" with this text every 3sec.

<script>

    const ispr = document.getElementById("ispr_1");

    var myFile = 'dynamic_data.txt';
    async function getUsers() {
        var response = await fetch(myFile, {cache: "reload"});
        if (response.ok) {
            var data = await response.text();
            ispr.textContent = data;
            return data;
        } else {
            alert('error', response.status);
        }
    }

    var intervalId = setInterval(getUsers, 3000);
</script>
1

There are 1 answers

0
Nicolas Pierrot On

Put your C file output somewhere in your server, and look at https://developer.mozilla.org/fr/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest