繁体   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