iframe刷新时更改URL

时间:2015-09-15 03:42:44

标签: javascript iframe page-refresh

我在里面写了一个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({{ 1}}),所以当时间到了,我看到page2.html里面有count.html的iframe,如何解决?

2 个答案:

答案 0 :(得分:2)

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

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

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

有关详细信息,请参阅Difference between window.location.href and top.location.href

答案 1 :(得分:1)

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

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