DIV CSS 佈局教程網

JS 頁面返回與刷新全整理:history、reload 與跨視窗應用
編輯:關於JavaScript     

「返回上一頁並刷新」看起來是個小需求,但一牽扯到 frame、彈出視窗、表單提交,寫法就差很多。這份筆記把常用句型依情境整理清楚。

一、最基本的返回與刷新

<a href="javascript:history.go(-1)">返回上一頁</a>
<a href="javascript:location.reload()">重新載入頁面</a>
<a href="javascript:history.go(-1);location.reload()">返回上一頁並重新載入</a>

要往前跳兩頁:

history.go(-2);
location.reload();

另一種思路是回到「上一個來源頁」,用 document.referrer 就等於回到真的那個轉來的地方:

<a href="#" onclick="self.location = document.referrer;">返回</a>

在伺服器端輸出同樣效果(ASP):

response.Write("<script language=javascript>self.location=document.referrer;</script>")

這招常用在表單 action 處理完之後,回到前一頁並刷新。

二、history 家族速查

  • history.go(-1):上一頁;history.go(-2):上兩頁
  • history.back():上一頁
  • window.history.forward():下一頁
  • window.history.go(n):跳到相對第 n 頁,也可傳入造訪過的 URL
<a href="javascript:history.go(-1);">回上一頁</a>

<script language=javascript>
if (!confirm('確認完成?')) { history.back(); }
</script>
<script language=javascript>history.go(-1);</script>

頁面跳轉:onclick="window.location.href='list.aspx'"

動態載入外部 js 的小技巧

<script type="text/javascript">
<!--
if (typeof SWFObject == "undefined") {
    document.write('<scr' + 'ipt type="text/javascript" src="/scripts/swfobject-1.5.js"></scr' + 'ipt>');
}
//-->
</script>

三、重新整理頁面的八種寫法

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

實務上以 location.reload() 最清楚;location.reload(true) 可要求略過快取重新抓資料(部分瀏覽器已忽略此參數)。

四、自動刷新與自動跳轉

純 HTML 就能做到,放進 <head>:

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

<!-- 20 秒後跳到指定網址 -->
<meta http-equiv="refresh" content="20;url=https://www.javaeye.com">

用 JavaScript 版本:

<script language="JavaScript">
function myrefresh() {
    window.location.reload();
}
setTimeout('myrefresh()', 1000);   // 每 1 秒刷新
</script>

提醒:不到必要不要對整個頁面做秒級自動刷新,這對使用者閱讀與伺服器負擔都不友善。

五、刷新父視窗(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>");

六、框架(frame)相關

// 刷新包含本框架的整個頁面
<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()">

七、在 ASP 中實作「返回並刷新」

先回報結果,再提供返回連結:

<%
response.write("<font color='red' size='12'>刪除成功!</font>")
%>
<div align="center">
[<a href="/" onClick="javascript:window.history.go(-1);return false;">返回</a>]
</div>

問題是單純 history.go(-1) 會把上一頁從快取還原,剛剛刪掉的項目還在畫面上。解法是在被返回的那一頁放一段自我刷新腳本,用 window.name 當旗標避免無限刷新:

<script>
if (window.name != "Bencalie") {
    // 第一次進來才刷新,並留下記號
    location.reload();
    window.name = "Bencalie";
} else {
    // 已經刷新過,什麼都不做,避免迴圈
    window.name = "";
}
</script>

更簡潔的做法是跳脫「返回」的框架,直接導向明確的頁面:

response.write "<script>alert('刪除成功!');window.location='原來的頁面';</script>"

這招的優點是流程單向、不會踩到快取;代價是使用者按瀏覽器上一頁時仍可能看到舊資料,若要徹底避免,可在伺服器端對該頁加上 no-cache 標頭。

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