DIV CSS 佈局教程網

JS 刷新框架內其他頁面與刷新窗口的方法匯總
編輯:關於JavaScript     

frameset 時代的頁面結構讓「刷新另一個框架裡的頁面」成為一個日常操作。本文用一個上分下結構的三頁面示例,把 window、parent、frames 之間的關係與常見刷新語句梳理清楚,並附上自動刷新與彈窗刷新的相關寫法。

示例結構

frame.html 由上(top.html)下(bottom.html)兩個頁面組成:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
  <TITLE>frame</TITLE>
</HEAD>
<frameset rows="50%,50%">
  <frame name="top" src="top.html">
  <frame name="bottom" src="bottom.html">
</frameset>
</HTML>

現在讓 top.html 提供七個按鈕,全部用來刷新下方的 bottom.html,看看總共有多少種等價寫法:

window.parent.frames[1].location.reload();
window.parent.frames.bottom.location.reload();
window.parent.frames["bottom"].location.reload();
window.parent.frames.item(1).location.reload();
window.parent.frames.item('bottom').location.reload();
window.parent.bottom.location.reload();
window.parent['bottom'].location.reload();

top.html 的按鈕部分:

<input type="button" value="刷新1" onclick="window.parent.frames[1].location.reload()"><br>
<input type="button" value="刷新2" onclick="window.parent.frames.bottom.location.reload()"><br>
<input type="button" value="刷新3" onclick="window.parent.frames['bottom'].location.reload()"><br>
<input type="button" value="刷新4" onclick="window.parent.frames.item(1).location.reload()"><br>
<input type="button" value="刷新5" onclick="window.parent.frames.item('bottom').location.reload()"><br>
<input type="button" value="刷新6" onclick="window.parent.bottom.location.reload()"><br>
<input type="button" value="刷新7" onclick="window.parent['bottom'].location.reload()"><br>

為了確認下方頁面確實被刷新,在 bottom.html 的 body 上掛一個 onload 彈窗:

<BODY onload="alert('我被加載了!')">
  <h1>This is the content in bottom.html.</h1>
</BODY>

概念澄清

window 指當前頁面本身,此例中即 top.html。
parent 指包含當前頁面的父框架頁面,此例中即 frame.html。
frames 是 window 對象上的一個數組,代表該框架內的所有子頁面,既可用下標索引,也可用 name 索引。
item 是 frames 集合的方法,用於按索引或名稱取回元素。

需要留意的是,如果某個子頁面本身又是一個框架頁,內層還有更深的子頁面,那麼上述部分寫法可能失效,此時要一層層往下取。

附:刷新頁面的常見語句

history.go(0)
location.reload()
location = location
location.assign(location)
document.execCommand('Refresh')
window.navigate(location)
location.replace(location)
document.URL = location.href

附:自動刷新與自動跳轉

<!-- 每 20 秒刷新一次 -->
<meta http-equiv="refresh" content="20">

<!-- 20 秒後跳轉到指定地址 -->
<meta http-equiv="refresh" content="20;url=https://www.cnblogs.com">
function myrefresh() {
  window.location.reload();
}
setTimeout('myrefresh()', 1000); // 指定 1 秒刷新一次

附:ASP.NET 輸出刷新父窗口腳本

this.response.write("<script>opener.location.reload();</script>");
this.response.write("<script>opener.window.location.href = opener.window.location.href;</script>");
Response.Write("<script language=javascript>opener.window.navigate('你要刷新的頁.asp');</script>")

附:JS 刷新框架的常用語句

// 刷新包含該框架的頁面
parent.location.reload();

// 子窗口刷新父窗口
self.opener.location.reload();

// 刷新同一層級的另一個框架
parent.另一FrameID.location.reload();

附:開窗與關窗時刷新

<body onload="opener.location.reload()">  開窗時刷新
<body onUnload="opener.location.reload()"> 關閉時刷新

或者在彈出窗口的 body 上直接寫 onUnload="window.opener.location.reload();",關閉彈窗時自動刷新父窗口。

實踐建議

frameset 與 frame 已在 HTML5 中被廢棄,新項目請改用 iframe 配合 postMessage 通信,或者直接轉向單頁應用。上面這些寫法在維護老系統時仍然有用,但要注意同源策略的限制:跨域 iframe 的 location 與 document 都是不可訪問的。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved