在框架(frameset)架構下,從一個子頁面刷新另一個子頁面、或刷新父視窗,是常見但需要記住幾種寫法的操作。以下用一個實例把各種語句一次講清楚。
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,以下七種語句都能達到目的:
語句1. window.parent.frames[1].location.reload();
語句2. window.parent.frames.bottom.location.reload();
語句3. window.parent.frames["bottom"].location.reload();
語句4. window.parent.frames.item(1).location.reload();
語句5. window.parent.frames.item('bottom').location.reload();
語句6. window.parent.bottom.location.reload();
語句7. 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 掛上 onload 提示,用來驗證確實被重新載入了:
<BODY onload="alert('我被加載了!')">
<h1> This is the content in bottom.html. </h1>
</BODY>
window 指當前頁面,此例即 top.html。
parent 指當前頁面的父頁面,也就是包含它的框架頁,此例即 frame.html。
frames 是一個類陣列物件,代表該框架內的所有子頁面。
item() 是方法,回傳集合中的元素。
要注意的是,若子頁面本身也是框架頁、內含更多層子頁,上述部分語法可能失效,得改用更精確的路徑逐層指定。
1 history.go(0)
2 location.reload()
3 location = location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)
7 location.replace(location)
8 document.URL = location.href
其中 location.reload() 語意最清楚,是首選;document.execCommand 已屬 deprecated,不建議再用。
把 meta 標籤放進 head 區域即可:
<!-- 每 20 秒刷新一次 --> <meta http-equiv="refresh" content="20"> <!-- 20 秒後跳轉到指定網址 --> <meta http-equiv="refresh" content="20;url=https://www.example.com">
或用 JavaScript 定時:
<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()', 1000); // 指定 1 秒刷新一次
</script>
提醒一句:自動刷新會打斷使用者操作,對無障礙與使用者體驗都不友善,除非有明確需求(例如即時儀表板),否則應避免。
1. this.Response.Write("<script>opener.location.reload();</script>");
2. this.Response.Write("<script>opener.window.location.href = opener.window.location.href;</script>");
3. Response.Write("<script language=javascript>opener.window.navigate('你要刷新的頁.asp');</script>")
// 刷新包含該框架的頁面 <script language=JavaScript> parent.location.reload(); </script> // 子視窗刷新父視窗 <script language=JavaScript> self.opener.location.reload(); </script> // 刷新另一個框架的頁面 <script language=JavaScript> parent.另一FrameID.location.reload(); </script>
開窗或關窗時刷新:
<body onload="opener.location.reload()"> 開窗時刷新 <body onUnload="opener.location.reload()"> 關閉時刷新 <script language="javascript"> window.opener.document.location.reload() </script>
在彈出視窗的 body 中加入 onUnload="window.opener.location.reload();",關閉彈出視窗時就會自動刷新父視窗。
最後補充:frameset 與 frame 已在 HTML5 中廢棄,新專案應改用 iframe,或使用 CSS Flexbox/Grid 做頁面分割。上面這些知識在維護舊系統時仍然用得上,但不必再擴充。