在我的网页上,我有一段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();
    }
  }


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

10-05 20:41
查看更多