当iFrame刷新时,iFrame中的Javascript文件不会被执行

时间:2017-04-20 05:01:26

标签: javascript html iframe

当我的iFrame刷新时,我的iframe中使用的javascript文件( app.js )是否有任何原因无法执行?

基本上现在发生的事情是: 当我第一次加载 index.html 时,javascript app.js 输出" hello world"到控制台,但是一旦iframe自动刷新(通过 reloader.js ),所有内容都会刷新,但我的意思是,一旦iframe刷新,我就不会刷新"你好世界"。

有没有人通过同样的问题?

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>treedux - Development Server</title>
    <script src="reloader.js"></script>

    <script>

    </script>
  </head>
  <body>
    <div class="topbar">
      <div class="icon-menu"></div>
        3dux.io Header
    </div>
    <iframe id="treeduxwrapper" class="iframe" src="iframe.html"></iframe>
  </body>
</html>

Iframe.html的

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>treedux - Development Server</title>
    <script type="text/javascript" src="app.js"></script>
  </head>
  <body>
    <div class="topbar">
      <div class="icon-menu"></div>
        CONTENT
    </div>

  </body>
</html>

app.js

console.log('hello world')

提前致谢, TF

2 个答案:

答案 0 :(得分:1)

尝试重新加载,

document.getElementById('treeduxwrapper').contentWindow.location.href = "iframe.html"

答案 1 :(得分:0)

我认为问题是你在index.html中保留了你的reloader.js,它只会重新加载当前页面(index.html),而不是iframe.html。因此,简而言之,iframe.html不会在每次重新加载index.html时重新加载。