在網頁中實作返回上一頁、前往下一頁或重新整理,通常可使用 window.history 與 location 物件。最常見的寫法包括 history.back()、history.go(-1)、history.forward() 與 location.reload()。
<input type="button" value="後退" onclick="window.history.back()" /> <input type="button" value="前進" onclick="window.history.forward()" /> <input type="button" value="重新整理" onclick="window.location.reload()" />
若在表單提交後需要返回上一頁,也可由伺服器端輸出 JavaScript。例如以 history.go(-2) 回到提交前的頁面,能減少使用者重複輸入資料的機會。
實際使用時,應依頁面流程決定要回到哪一頁,避免因為歷史紀錄過多而導致的跳轉錯誤。