在每秒钟更新的网页上显示一个txt文件 [英] Show a txt file on a webpage which updates every second

查看:79
本文介绍了在每秒钟更新的网页上显示一个txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的黑暗中拍摄,我不知道如何做到这一点,所以一些有用的教程指针和/或链接将是伟大的:



我有一个网站,我想显示一个文本文件服务器日志)。可能嵌入。问题是,只要服务器中发生事件(通常比半秒快),就会更新此文件。我怎样才能让网页实时显示文件,这意味着显示文件的实时提要?



我的猜测是它会使用javascript和AJAX,但是我对这两方面的知识都非常有限。
任何指针和帮助将不胜感激:)

解决方案

我的答案使用PHP和Ajax,尽管更改为ASP或任何其他语言不会很难。
在头上

 < script type =text / javascript> 

函数Ajax()
{
var
$ http,
$ self = arguments.callee;

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


$ b $ 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>





在Body

 < script type =text / javascript> 
setTimeout(function(){Ajax();},1000);
< / script>
< div id =ReloadThis>预设文字< / div>

< / body>



现在使用loadtxt.php读取文本文件的值

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


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 a text file (server log). Probably embedded. The problem is, this file is updated whenever events happen in the server (faster than half a second usually). How can I make it so the webpage displays the file in real time, meaning showing a live feed of the file?

My guess is that it would use javascript and AJAX but my knowledge on both are pretty limited. Any pointers and help would be appreciated :)

解决方案

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

    <script type="text/javascript">

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

            if (window.XMLHttpRequest) {
                $http = new XMLHttpRequest();
            } else if (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

    <script type="text/javascript">
        setTimeout(function() {Ajax();}, 1000);
    </script>
    <div id="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;
        }
    ?>

这篇关于在每秒钟更新的网页上显示一个txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆