简体   繁体   English

使用 static html 页面自动刷新 div 内容

[英]Auto-Refresh div content with static html page

I want to reload html div tag in every 5 minutes which call.js file in it without refresh entire page.我想每 5 分钟重新加载 html div 标签,其中 call.js 文件在其中不刷新整个页面。

<div id="pie">
  <script src="index.js">// reload in every 5 minutes
     </script>
 </div>

If you are looking to get the script to refresh without reloading the entire page, try using an XHR for asynchronous loading, and then - shudder - use the eval function to run your new javascript only if you trust the content in the script because the eval function can be badly misused by your client for XSS attacks.如果您希望在不重新加载整个页面的情况下刷新脚本,请尝试使用eval进行异步加载,然后 -不寒而栗-仅当您信任脚本中的内容eval function 可能会被您的客户端严重滥用以进行 XSS 攻击。

Here is an example of a 5-minute interval in which the script is gathered and run each time (remember this will only work if your file is on the same website abiding by the CORS policy)以下是每次收集和运行脚本的 5 分钟间隔示例(请记住,这仅在您的文件位于遵守 CORS 策略的同一网站上时才有效)

setInterval(function () {

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       eval(xhr.responseText); //ONLY IF YOU TRUST THE FILE
    }
};
xhr.open("GET", "index.js", true);
xhr.send();

}, 5 * 1000);

Check out this website for more information on XMLHttpRequests: https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp查看此网站以获取有关 XMLHttpRequests 的更多信息: https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

They even have tips on how to avoid cached files他们甚至有关于如何避免缓存文件的提示W3Schools XHR 示例

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

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