簡體   English   中英

JavaScript重新加載功能無法正常工作

[英]JavaScript Reload Function Not Working Properly

在我的網頁上,我有一段JavaScript可以每三秒鍾重新加載/刷新iframe。

window.setInterval(function() {
      reloadIFrame()
  }, 3000);

  function reloadIFrame() {
    var frame = document.getElementById("iframe");
    var len = frame.getElementsByTagName("TABLE").length;
    if ( len == 0 ){
      console.log('reloading..');
      document.getElementById('iframe').contentWindow.location.reload();
    }
  }

但是,我不希望該功能在iframe中存在表時起作用,而在有表時它仍然運行。 請讓我知道我是否缺少什么或您對替代解決方案的建議。

(我確實相信我要引用的iframe在localhost:8000上是本地的。如果這很重要,我正在與Django合作,這是模板的一部分。)

對於任何有類似問題的人:

正如charlietfl對原始帖子的評論一樣,未引用contentWindow 我的代碼在下面進行了修改,現在可以正常工作:

window.setInterval(function() {
      reloadIFrame()
  }, 3000);

  function reloadIFrame() {
    var frame = document.getElementById("iframe");
    var len = frame.contentWindow.document.getElementsByTagName("TABLE").length;
    if ( len == 0 ){
      console.log('reloading..');
      document.getElementById('iframe').contentWindow.location.reload();
    }
  }

我只需要在frame之后添加contentWindow.document

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM