Skip to content Skip to sidebar Skip to footer

Show A Txt File On A Webpage Which Updates Every Second

I'm sort of shooting in the dark here; I have no knowledge how to do this so some pointers and/or links to helpful tutorials would be great: I have a website that I want to display

Solution 1:

My answer uses PHP and Ajax though changing to ASP or any other language wont be hard. In the head

    <script type="text/javascript">

        functionAjax()
        {
            var$http,
                $self = arguments.callee;

            if (window.XMLHttpRequest) {
                $http = new XMLHttpRequest();
            } elseif (window.ActiveXObject) {
                try {
                    $http = new ActiveXObject('Msxml2.XMLHTTP');
                } catch(e) {
                    $http = new ActiveXObject('Microsoft.XMLHTTP');
                }
            }

            if ($http) {
                $http.onreadystatechange = function()
                {
                    if (/4|^complete$/.test($http.readyState)) {
                        document.getElementById('ReloadThis').innerHTML = $http.responseText;
                        setTimeout(function(){$self();}, 1000);
                    }
                };
                $http.open('GET', 'loadtxt.php' + '?' + new Date().getTime(), true);
                $http.send(null);
            }

        }

    </script>

In the Body

<scripttype="text/javascript">setTimeout(function() {Ajax();}, 1000);
    </script><divid="ReloadThis">Default text</div></body>

Now using loadtxt.php read the values of the text file

<?php$file = "error.txt";
        $f = fopen($file, "r");
        while ( $line = fgets($f, 1000) ) {
            print$line;
        }
    ?>

Solution 2:

Using jQuery, you could do the following

setInterval(function() {
    $('#element').load('/url/to/file');
}, 1000);

Would refresh the div with ID element with the file contents every 1 second

Solution 3:

You could use jQuery .get to get the file every few seconds and update the page to show the contents.

Solution 4:

Others have talked about loading the log file every refresh but depending on the size of the file this migth be a bad idea. You might want to create a server side page that will read the log file and keep track of how much of it has already been given to you and only give you the new bits. If its a 10k file it would be annoying (and potentially laggy) to have this transferred to you every second.

Otherwise other people seem to have covered most of the client side stuff.

Solution 5:

There are various ways of doing this...

You could look into long polling.

Stick a meta refresh tag to refresh the page every X seconds.

tail -f /path/to/log.log in terminal will open a live preview of the last few lines of that file - this is what I do if I need to read the error logs as I debug.

Or simply refresh the page manually as you go, it might be annoying having the page change it's contents automatically.

As you have said your file is very large, I would use the PHP file() function to just grab the first X amount of lines from a file to keep bandwith down and readability up!

Post a Comment for "Show A Txt File On A Webpage Which Updates Every Second"