在每秒钟更新的网页上显示一个txt文件 [英] Show a txt file on a webpage which updates every second
问题描述
我有一个网站,我想显示一个文本文件服务器日志)。可能嵌入。问题是,只要服务器中发生事件(通常比半秒快),就会更新此文件。我怎样才能让网页实时显示文件,这意味着显示文件的实时提要?
我的猜测是它会使用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屋!