简体   繁体   English

当其他文件的内容更改时,如何刷新HTML文件?

[英]How do I refresh an HTML file when the contents of a different file change?

I have this situation with a device accessing a simple MySQL database using PHP. 我遇到这样的情况:设备使用PHP访问简单的MySQL数据库。 When I do a search, the results are written in an HTML file. 当我进行搜索时,结果将写入HTML文件中。

I have a second device (very basic android tablet) that has this HTML file opened. 我还有第二个设备(非常基本的Android平板电脑)已打开此HTML文件。 Using live.js it refreshes when it's contents change. 使用live.js可以在内容更改时刷新。 What I need is this: a static message saying something like "Waiting for search results" and only when the contents of the page change the results are shown... 我需要的是:一条静态消息,上面写着“等待搜索结果”之类的内容,并且仅在页面内容更改结果时才显示...

Something like this: On the tablet load 1.html (waiting for search results). 这样的事情:在平板电脑上加载1.html(等待搜索结果)。 When contents of page 2.html change (there was a search query), display that (2.html). 当2.html页的内容发生更改(有搜索查询)时,将其显示(2.html)。 After a push of a button (user input), let's go back to 1.html and wait for the contents of page 2.html to change again. 按下按钮(用户输入)后,让我们回到1.html,等待页面2.html的内容再次更改。

With live.js alone I can only watch for the file that's currently open... 仅使用live.js时,我只能监视当前打开的文件...

Any idea on how I can achieve this? 关于如何实现此目标的任何想法吗? Thanks a bunch. 谢谢你

[EDIT - SOLVED] I got this to work like this: when the user of the tablet pushes a button it runs a PHP script that replaces the contents of the file (the previous search results) with the "waiting for results" text. [编辑-已解决]我的工作方式是这样的:平板电脑的用户按下按钮时,它将运行PHP脚本,该脚本将文件内容(先前的搜索结果)替换为“等待结果”文本。 Of course, it has live.js in the head, and when a new search occurs, the page reloads with the new search results. 当然,它的头部带有live.js,并且在进行新搜索时,页面会重新加载新搜索结果。

It was easier than I thought I was overthinking it. 这比我认为的要容易得多。

The PHP script: PHP脚本:

<?php
ob_start();

$myFile = 'ana.html';
unlink($myFile);
$fh = fopen($myFile, 'w') or die ("can't open file");

$stringData = 
'<html>
  <head>
  <title>Rezultatele cautarii</title>
   <link rel="stylesheet" href="style.css">
   <script src="live.js"></script>
  </head>
  <body>
   <h1 class="button">Asteptam cererea</h1>
  </body>
 </html>';

fwrite($fh, $stringData);
fclose($fh);

header('Location: '.$myFile);

die();
?>

Do you mean something like this? 你的意思是这样吗?

document.write('<div id="loading"><br><br>Please wait...</div>');


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  document.getElementById("loading").style.display="none";
});

I suppose you could have a setInterval() running in JavaScript, then get the content of the page as it was at a previous interval and compare it in PHP using a checksum? 我想您可以在JavaScript中运行setInterval(),然后获取页面内容,就像以前的间隔一样,然后在PHP中使用校验和进行比较? Or just compare it letter by letter. 或者只是逐字母比较它。 Too tired to write code now though. 现在太累了,无法编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM