如何在JavaScript中刷新它而不影响HTML? [英] How to make it refresh in JavaScript and not affect the HTML?

查看:70
本文介绍了如何在JavaScript中刷新它而不影响HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码工作超级好但我不知道如何将JavaScript放入其中以检查我的表聊天中是否有一个额外的结果,如果有它应刷新页面,如果结果没有改变,然后没有刷新页面,对不起,但我不知道什么时候谈到JavaScript。

I have a code that is working super ok but I don't know how it to put JavaScript into it to check if there is one extra result in my table "chat" and if there is it should refresh the page and if the result hasn't changed then not to refresh page, sorry but I have no idea when it comes to JavaScript.

chat.php

require('checkformessages.php');
...

checkformessages.php

checkformessages.php

$sqlmessages = mysql_query("SELECT * FROM chat");
$numbermessages = mysql_num_rows($sqlmessages);


// JavaScript script (no idea what it's supposed to be
// but some kind of timer checking if there is new result in the database)

if($numbermessages===$Javascriptcheck) {
//do nothing
} else {
//it should refresh the chat.php because its required in the page
echo "<script>window.location = window.location.href;</script>";
}


推荐答案

这太宽泛了,并且可以做更多的研究。但是,我不介意添加一些注释以使您走上正轨:

This is rather too broad, and could do with some more research. However, I don't mind adding some notes to get you on the right track:


  1. AJAX请求来自浏览器。如果您定期检查某些内容,可能需要一个向服务器发出请求的JavaScript计时器。

  2. 由于这是一个读取操作,因此 get 操作没问题。您的脚本可能会从查询字符串中读取项目,然后使用合适的消息格式进行响应。 JSON目前很受欢迎。

  3. 您的返回数据可能是最好的新消息数组。你确实可以返回一个布尔值,如果存在新行则刷新整个页面,但这相当浪费。您也可以在AJAX回复中返回新行。

  4. 要获取新行,您需要在此表中添加时间戳,并将最后已知的时间戳发送到服务器使用AJAX操作。新行是时间戳大于或等于提供的值。

  5. jQuery get()方法可能是适合的。

  1. AJAX requests originate from the browser. If you are periodically checking something, perhaps you need a JavaScript timer that makes a request to the server.
  2. Since this is a read operation, a get operation is fine. Your script may read items from the query string and then respond with a suitable message format. JSON is popular these days.
  3. Your returned data would probably be best as an array of new messages. You can indeed return just a boolean, and refresh the whole page if new rows exist, but this is rather wasteful. You might as well just return new rows in the AJAX reply.
  4. To get just new rows, you'll need to a timestamp in this table, and send the last known timestamp to the server with the AJAX operation. The new rows are the ones that have a timestamp larger or equal to the supplied value.
  5. The jQuery get() method is probably suitable.

我建议使用上面的内容来帮助你入门 - 你可能需要用搜索引擎研究它们,我会鼓励。如果你仍然遇到困难,在线完成一些示例/教程可能也会有所帮助。

I would recommend using the above to get you started - you may need to research each with a search engine, which I would encourage. If you still get stuck, working through some examples/tutorials online might also be helpful.

祝你好运!

这篇关于如何在JavaScript中刷新它而不影响HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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