网页浏览器没有显示一个txt文件的更新版本

网页浏览器没有显示一个txt文件的更新版本

本文介绍了网页浏览器没有显示一个txt文件的更新版本,但打开一个新的浏览器显示正确的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PHP一起尝试使用Ajax和jQuery做一个网站,我可以写一个文本文件,然后在网站加载文本文件的内容到一个div。

I'm experimenting with PHP together with Ajax and JQuery to make a site where I can write to a text file and then have the site load the textfile's contents into a div.

这是JQuery的code我使用。

This is the JQuery code I'm using.

$(document).ready(function() {
    setInterval(function() {
        $("#text").load("storydoc.txt");
    }, 2000);
});

这里是PHP连同有关HTML。

And here is the PHP together with the relevant HTML.

<div id="text">
</div>
<form id="form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <input id="wordinput" class="input" type="text" name="word" maxlength="30"></input>
    <input id="submit" class="hidden" type="submit" name="submit"></input>
</form>
<?php
    if(isset($_POST['submit'])) {
        $word = " " . $_POST['word'];
        $ret = file_put_contents('text.txt', $word, FILE_APPEND | LOCK_EX);
        if($ret === false) {
            die("There was an error writing this file");
        }
    }
?>

那么,它的作用是它可以让用户写的text.txt使用#wordinput比更新#text与.load()来显示从的text.txt的新更新的文本。此文随后可以看到和写入由其他用户,以及

So what it does is it lets the user write to "text.txt" using #wordinput and than updates #text with .load() to show the new updated text from "text.txt". This text can then be seen and written to by other users as well.

我遇到的问题是,该文本文件可以显示在不同的浏览器不同的内容。如果我例如打开该网站在Firefox和写东西到text.file,它不会显示。即使我去了文件本身的cPanel文件管理器里面它仍然显示的文字,因为它是当我在网站上。如果我再打开浏览器并转到网站出现,它会显示两个网站和文件管理器的新更新的文本。编写更多的话也立即显示在网站上的Chrome浏览器。与此同时火狐仍然显示,即使刷新后同样过时的文本文件的内容。在此之后它完美的作品在所有浏览器,但谁第一个打开了该文件第一个。然后,如果你离开了文本文件未经编辑的一段时间,我再次得到了同样的问题,不必切换浏览器。

The problem I'm having is that the text file can show different contents on different browsers. If I for example open up the site in Firefox and write something to the text.file, it won't show. Even if I go to the file itself inside the cPanel file manager it still shows the text as it were when I got on the site. If I then open up Chrome and go to the site there, it will show the new updated text on both the site and file manager. Writing more words also instantly shows on the site in Chrome. Meanwhile Firefox is still showing the same outdated textfile contents even after refreshing. After this it works perfectly on all browsers but the one who first opened up the file first. Then if you leave the text file unedited for some time I get the same problem again, having to switch browser.

它的工作原理相同的方式倒过来,开始与Chrome浏览器,所以我不认为这有什么用特定的网页浏览器。对我来说,这似乎是谁首先打开浏览器的text.txt保存它的一个副本,并只使用一个从那里出来,我不知道如何解决这个问题。

It works the same way the other way around, starting with Chrome, so I don't think it has anything to do with a specific webbrowser. To me it seems like the browser who first opens up "text.txt" saves a copy of it and only uses that one from there on out, and I have no clue how to solve it.

任何解决方案或建议,欢迎!预先感谢您!

Any solutions or ideas are welcomed! Thank you in advance!

推荐答案

附加一个时间字符串,以在负载语句prevent缓存文件的标题 -

Append a time string to the title of the file in the load statement to prevent caching -

$(document).ready(function() {
    setInterval(function() {
        var ms = (new Date).getTime(); // epoch time
        $("#text").load("storydoc.txt?ms=" + ms ); // add query string
    }, 2000);
});

所添加的字符串是无关紧要的实际文件,但将有发出请求新的每一次因为加入的时间字符串将是不同的,并且不会有在高速缓存中的副本的影响

The added string is inconsequential to the actual file but will have the affect of making the request 'new' each time because the added time string will be different and there will not be a copy in the cache.

这篇关于网页浏览器没有显示一个txt文件的更新版本,但打开一个新的浏览器显示正确的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:30