JavaScript Window History


window.history 對象包含流覽器的歷史。


Window History

window.history對象在編寫時可不使用 window 這個首碼。

為了保護用戶隱私,對 JavaScript 訪問該對象的方法做出了限制。

一些方法:

  • history.back() - 與在流覽器點擊後退按鈕相同
  • history.forward() - 與在流覽器中點擊向前按鈕相同

Window history.back()

history.back() 方法加載歷史列表中的前一個 URL。

這與在流覽器中點擊後退按鈕是相同的:

實例

在頁面上創建後退按鈕:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <head> <script> function goBack() { window.history.back() } </script> </head> <body> <input type="button" value="Back" onclick="goBack()"> </body> </html>

以上代碼輸出為:



Window history.forward()

history forward() 方法加載歷史列表中的下一個 URL。

這與在流覽器中點擊前進按鈕是相同的:

實例

在頁面上創建一個向前的按鈕:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function goForward() { window.history.forward() } </script> </head> <body> <input type="button" value="Forward" onclick="goForward()"> </body> </html>

以上代碼輸出為: