【问题标题】:Change URL when iframe refreshiframe 刷新时更改 URL
【发布时间】:2015-09-15 03:42:43
【问题描述】:

我写了一个页面,里面有 iframe,结构如下:

count.html:

/* http://example.com/count.html */
<div class="primary">
     <h2>Countdown</h2>
          <div class="content">
               <iframe src="page2.html" id="iframe"></iframe>
          </div>
</div>

index.html:

/* http://example.com/index.html */
<div class="primary">
     <h2>Home</h2>
          <div class="content">
               <iframe src="home.html" id="iframe"></iframe>
          </div>
</div>

page2.html:

<head>
<script>
var count = "<!--# echo time -->"; /* C code will control it*/
function display() {
    if (wait_seconds == null) {
        wait_seconds = (count == "" ? 180 : parseInt(count));
    }
    document.countdown.b.value = wait_seconds;
    if (wait_seconds <= 0) {
        if(redirect == null || redirect == "")
            redirect = "index.html";
        location.href = redirect;
        return;
    } else {
        wait_seconds = wait_seconds-1;
    }
    window.setTimeout("display()", 1000);
}
</script>
</head>
<body>
    <form name='countdown'>
        <h2>Countdown Bar</h2>
            <input type='text' size='2' name='b' disabled>
    </form>
    <script>
        display();
    </script>
</body>

我在 iframe 中设计了一个倒计时栏(page2.html) ,时间到了,我想count.html刷新并更改到默认页面index.html,但是当页面刷新时,它卡在count.html并且只刷新iframe(page2.html),所以当时间到了,我看到了count.html有一个iframe,里面有index.html,怎么解决?

【问题讨论】:

  • 我在任何地方都没有看到countdown.html
  • @TomSarduy 已修复,谢谢

标签: javascript iframe page-refresh


【解决方案1】:

[window.]location.href控制当前帧的位置。

使用[window.]top.location.href 控制最顶层文档的位置。

location.href = redirectURL; 更改为top.location.href = redirectURL;

更多详情请见Difference between window.location.href and top.location.href

【讨论】:

    【解决方案2】:

    这是另一种不使用 javascript 的方法,将下面描述的元标记放入 count.html 中的 head 标记中

    <meta http-equiv="Refresh" content="5; url=index.html" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多