简体   繁体   English

当 div 内容改变时刷新浏览器

[英]Refresh browser when div contents changed

within the <div> I embedded an <img> , sometimes I have the problem that the browser didn't reload when new img came.<div>我嵌入了一个<img> ,有时我会遇到新 img 来时浏览器没有重新加载的问题。 So I refresh the browser within a specific time intervall.所以我在特定的时间间隔内刷新浏览器。 But how can I force the browser automatically refresh, when <div> contents changed?但是,当<div>内容更改时,如何强制浏览器自动刷新? I use Internet Explorer and don't want to use Ajax or Jquery.我使用 Internet Explorer 并且不想使用 Ajax 或 Jquery。 It would be great if we find a solution in Javascript or Html only.如果我们只在 Javascript 或 Html 中找到解决方案,那就太好了。

<body onload="JavaScript:AutoRefresh(5000);">

then in <script> section I set to window.location.reload(1)然后在<script>部分我设置为window.location.reload(1)

For this solution is not user-friendly.因为这个解决方案不是用户友好的。

Hope you find a better solution.希望你能找到更好的解决方案。

尝试改变形象?

<img src="./img.src" onchange="javascript:window.location.reload();"

Looks like this means you are changing the image on the webserver, and want the browser to automatically reload.看起来这意味着您正在更改网络服务器上的图像,并希望浏览器自动重新加载。 But the problem is that the browser would not know when the image has changed, unless you reload.但问题是浏览器不会知道图像何时更改,除非您重新加载。

You can use a http request to poll the server.您可以使用 http 请求轮询服务器。

It is not clear what way the image is updated, but I'd do this:目前尚不清楚图像的更新方式,但我会这样做:

  • any time the image is changed put the hash or last update timestamp under a URL on my server每当更改图像时,将哈希或上次更新时间戳放在我服务器上的 URL 下
  • use XMLHttpRequest (eg: https://stackoverflow.com/a/4033310/ ) to poll the hash/timestamp from the server every x seconds or minutes使用XMLHttpRequest (例如: https : //stackoverflow.com/a/4033310/ )每隔 x 秒或分钟从服务器轮询哈希/时间戳
  • compare the received data with the last known value将接收到的数据与最后一个已知值进行比较
  • reload the page if it is different如果不同,请重新加载页面

This way the page would only reload, when the image has changed.这样页面只会在图像更改时重新加载。

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

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